Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.