Jump to content


Photo

random date


  • Please log in to reply
3 replies to this topic

#1 eranwein

eranwein
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 14 March 2006 - 11:09 AM

is there any function that returns a random date between two givven dates?

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 14 March 2006 - 11:26 AM

work out a random date between start of year and today


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);

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
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 eranwein

eranwein
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 14 March 2006 - 11:49 AM

thanks. works great.

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

thanks again, eran

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 14 March 2006 - 11:59 AM

[!--quoteo(post=354844:date=Mar 14 2006, 11:49 AM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Mar 14 2006, 11:49 AM) View Post[/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
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users