Jump to content

random date


eranwein

Recommended Posts

work out a random date between start of year and today

[code]

function getrandomdate($date1, $date2, $format = "Y-m-d")
{
   $randomdate = rand(strtotime($date1), strtotime($date2));
   return date($format, $randomdate);
}

// mysql date format, although strtotime works on many date formats.
$firstdate = "2006-01-01";
$lastdate = "2006-03-14";


echo "random date is ".getrandomdate($firstdate, $lastdate);
[/code]

give that a go. to get the date back in a different format, just add a third parameter to the call to 'getrandomdate' function, else it'll come back in mysql format.

Cheers
Mark
Link to comment
https://forums.phpfreaks.com/topic/4911-random-date/#findComment-17316
Share on other sites

[!--quoteo(post=354844:date=Mar 14 2006, 11:49 AM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Mar 14 2006, 11:49 AM) [snapback]354844[/snapback][/div][div class=\'quotemain\'][!--quotec--]
thanks. works great.

to all the others who reads it, notice that there is a missing $ on date2.

thanks again, eran
[/quote]

oops, my bad. i changed it now

Cheers
Mark
Link to comment
https://forums.phpfreaks.com/topic/4911-random-date/#findComment-17320
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.