开发者

How can I generate a random date in the past?

开发者 https://www.devze.com 2023-04-04 08:11 出处:网络
using PHP. It has to be a valid one, like: 2001-6-28 14:20:29 ? 开发者_运维问答 (and if possible not older than 10 years)unix timestamp is an integer from 0 to n so you can just use the normal rand

using PHP.

It has to be a valid one, like:

2001-6-28 14:20:29

?

开发者_运维问答

(and if possible not older than 10 years)


unix timestamp is an integer from 0 to n so you can just use the normal random method in php :)

$timestamp = rand(0, time() - 60*60*24*365*10);

// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A', $timestamp);


Get the seconds since the epoch for the two dates in question. Generate a random number between that range. Then convert back into a date.


$date = mt_rand(strtotime('-10 years'), time());

This will get you a unix timestamp, use date() to reformat it; and next time do the research yourself as this is a most basic question.

0

精彩评论

暂无评论...
验证码 换一张
取 消