How to generate random date between two dates usin

2020-01-24 21:09发布

I am coding an application where i need to assign random date between two fixed timestamps

how i can achieve this using php i've searched first but only found the answer for Java not php

for example :

$string = randomdate(1262055681,1262055681);

13条回答
孤傲高冷的网名
2楼-- · 2020-01-24 21:41

Here's another example:

$datestart = strtotime('2009-12-10');//you can change it to your timestamp;
$dateend = strtotime('2009-12-31');//you can change it to your timestamp;

$daystep = 86400;

$datebetween = abs(($dateend - $datestart) / $daystep);

$randomday = rand(0, $datebetween);

echo "\$randomday: $randomday\n";

echo date("Y-m-d", $datestart + ($randomday * $daystep)) . "\n";
查看更多
登录 后发表回答