Php to automatically increase date after date has been reached?

8 replies
Hi,

I'd like to make an "special offer" on a site that lasts 1 week and after this week is over it gets extended for another week and so on.

How can you do this in PHP?
#automatically #date #increase #php #reached
  • Profile picture of the author ToneUK
    If it is just to display a date that expires in a week then I would put the expiry date in a MySQL database or a text file if you do not have a database. I would then create a script that queries the database to see if the current date matches the date in the database or file.

    If the date matches or is greater then I would update the expiry date in the database or text file to a new 1 week expiry. If it is not then I would display the date that is in the database or file.

    There maybe better ways to do this which someone else will point out I'm sure but it's the first thing that comes to mind.
    Signature
    Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
    {{ DiscussionBoard.errors[4027035].message }}
    • Profile picture of the author paulpalm
      To store the start date...

      file_put_contents('filename', time());

      To check 7 days...

      $timeStart = file_get_contents('filename');
      $timeNow = time();
      if($timeNow > $timeStart + (7 * 24 * 60 * 60)) {
      // ... do stuff here now that 7 days have elapsed.
      }


      Should get you started ;-)

      However, as Istvan pointed out, it is best you create a new offer after day 7. Simply resetting the timer isn't righteous, but this is for you to judge.
      {{ DiscussionBoard.errors[4027213].message }}
      • Profile picture of the author ToneUK
        Was bored so I wrote this:

        <?php
        // get the expiry date from the text file
        $handle = fopen('expiry.txt', 'r') or die("can't open file");
        $expiry_date = fread($handle, filesize('expiry.txt'));
        fclose($handle);

        $now = date('d-m-Y');

        // check to see if the current date matches or is over
        if (check_date($now, $expiry_date)) {
        // TRUE so update the text file and show new expiry

        // generate the new date
        $newdate = date('d-m-Y', strtotime('+7 days'));

        // save to file
        $handle = fopen('expiry.txt', 'w') or die("can't open file");
        fwrite($handle, $newdate);
        fclose($handle);

        // show new date
        print $newdate;
        }
        else {
        // FALSE so show expiry date
        print $expiry_date;
        }

        // function to check if current date is greater than expiry
        function check_date($start_date, $end_date) {
        $start = strtotime($start_date);
        $end = strtotime($end_date);
        if ($start - $end > 0) {
        return 1;
        }
        else {
        return 0;
        }
        }
        ?>


        Just put the code in a new PHP file and create a new text file called expiry.txt. In the expiry.txt file put the expiry date in like this: 14-06-2011 and then save the file. Upload both files so that they are in the same directory on the server.
        Signature
        Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
        {{ DiscussionBoard.errors[4027371].message }}
  • Profile picture of the author Istvan Horvath
    There are ways and any php programmer can help you with that... the question is: should you do it?

    What you plan is called "fake scarcity" and if people realize that your offer is still alive after one week - you may lose any credibility you have.
    Signature

    {{ DiscussionBoard.errors[4027178].message }}
  • Profile picture of the author enigma2k
    Thanks very much for all these replies - have to try it out and hope it won't be too complicated for a php beginner.
    {{ DiscussionBoard.errors[4029233].message }}
    • Profile picture of the author ryanstreet
      Why not create a script that rotates offers instead? After 7 days a new deal is presented. You can also have upcoming previews. So if a person doesn't like what you have that week, they can come back and see what you have the next week.

      I hope this helps.
      Signature
      Ryan Street
      PHP Developer Specializing in WordPress and Magento
      {{ DiscussionBoard.errors[4038398].message }}
  • Profile picture of the author enigma2k
    @ryanstreet

    This sounds very interesting. Is there any ready-to-go script that offers this?
    {{ DiscussionBoard.errors[4043940].message }}
  • Profile picture of the author ionisis
    If you always want it to STAY at 1 week, you could use date() and strftime() to always show a time 1 week from now. If you want it to appear that the time IS counting down, you could store their visit date in a cookie, and calculate against that on subsequent visits.
    {{ DiscussionBoard.errors[4057701].message }}

Trending Topics