我怎样才能在过去生成一个随机日期?

问题描述:

using PHP.

It has to be a valid one, like:

2001-6-28 14:20:29

?

(and if possible not older than 10 years)

使用PHP。 p>

它必须是有效的,例如: p>

2001-6-28 14:20:29 code> p>

? p>

(如果可能的话,不超过10年) p> div>

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.