Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Please help! PHP create date (https://www.warriorforum.com/programming/68329-please-help-php-create-date.html)

angelus_1984 24th March 2009 02:29 PM

Please help! PHP create date
 
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.

GB2008 24th March 2009 03:23 PM

Re: Please help! PHP create date
 
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

GB2008 24th March 2009 03:24 PM

Re: Please help! PHP create date
 
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

angelus_1984 25th March 2009 05:34 AM

Re: Please help! PHP create date
 
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.

GB2008 25th March 2009 05:54 AM

Re: Please help! PHP create date
 
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.


All times are GMT -6. The time now is 07:09 PM.