How do you go about listing random dates (from earliest to newest)?
I’ve been trying to accomplish this with PHP but with no success. I can get a random date to show up but keeps looping the same one over and over again rather than creating new ones and listing them in the order specified.
This is my code so far:
// Create a random date between 2 months
$datestart = strtotime('01-11-2012');
$dateend = strtotime('01-12-2012');
$daystep = 86400;
$datebetween = abs(($dateend - $datestart) / $daystep);
$randomday = rand(0, $datebetween);
for($i=0; $i< rand(10, 30) ;++$i)
{
echo "<div>" . date("d/m/Y", $datestart + ($randomday * $daystep)) ."</div>";
}
Update: I’ve now managed to get the code working thanks to Mihai Iorga but there is still the issue of sorting the dates from oldest to newest. How do I achieve this:
// Create a random date between 2 months
$datestart = strtotime('01-11-2012');
$dateend = strtotime('01-12-2012');
$daystep = 86400;
$datebetween = abs(($dateend - $datestart) / $daystep);
$randomday = rand(0, $datebetween);
for($i=0; $i< rand(10, 30) ;++$i)
{
$randomday = rand(0, $datebetween);
echo "<div>" . date("d/m/Y", $datestart + ($randomday * $daystep)) ."</div>";
}
Tested code on http://writecodeonline.com/php/ and running this outputs: