Please help! PHP create date

4 replies
Hi everybody,

does anyone know a php script, which does not create the current date but the date of some days ago? If this script would randomly go back some days, like one guy sees the date 3 days ago another 6 days ago and so on, that would be perfect.

Thanks in advance.
#create #date #php
  • Profile picture of the author GB2008
    Not sure where you're going with this, but...

    <?php
    $countback = rand(1,30);
    echo "Counting back ".$countback." days<br>";
    $thePHPDate = getdate(time()); // Covert to Array
    $thePHPDate['mday'] = $thePHPDate['mday']-$countback; // subtract our random number
    $timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']); // Convert back to timestamp
    echo date("r", $timeStamp);
    ?>


    Just change the parameters in rand() to change the range of days you go back by. This is up & running here
    Signature
    {{ DiscussionBoard.errors[636021].message }}
  • Profile picture of the author GB2008
    Not sure where you're going with this, but...

    <?php
    $countback = rand(1,30);
    echo "Counting back ".$countback." days<br>";
    $thePHPDate = getdate(time()); // Covert to Array
    $thePHPDate['mday'] = $thePHPDate['mday']-$countback; // subtract our random number
    $timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']); // Convert back to timestamp
    echo date("r", $timeStamp);
    ?>


    Just change the parameters in rand() to change the range of days you go back by. This is up & running here
    Signature
    {{ DiscussionBoard.errors[636022].message }}
    • Profile picture of the author angelus_1984
      Thanks so much. That was almost exactly where I was going.

      Just one thing... After the time it always says +0100... I am from Germany so perhaps I don't get it. But does it refer to the different time zones in the US? If so could I change the script to just say EST or s.th. like that after the time?

      Thanks again.
      {{ DiscussionBoard.errors[637722].message }}
  • Profile picture of the author GB2008
    All you need to do is change the format parameter in the date() function. "r" defines the standard UTC timestamp - so that +0100 means the time displayed is 1 hour ahead of UTC - which sounds right for a German server to me.

    For all the possible formatting options - take a look at php.net and search for date() - there's some great information and worked examples on that website.
    Signature
    {{ DiscussionBoard.errors[637779].message }}

Trending Topics