PHP生成随机日期

时间:2014-08-02 08:32:21

标签: php

我想要一个生成随机时间的PHP脚本:

例如

first time : 2014-02-14 10:21:02 next time : 2014-02-14 08:21:02

只有时间更改日期不会改变。

3 个答案:

答案 0 :(得分:3)

$first_date = "2014-02-14 10:21:02";
$second_date = "2014-02-14 08:21:02";

$first_time = strtotime($first_date);
$second_time = strtotime($second_date);

$rand_time = rand($first_time, $second_time);
$rand_date = date('Y-m-d g:i:s', $rand_time);

echo $rand_date;

<强> DEMO

更新:今天的随机日期

$first_time = strtotime(date('Y-m-d'));
$second_time = $first_time+86400;

$rand_time = rand($first_time, $second_time);
$rand_date = date('Y-m-d g:i:s A', $rand_time);

echo $rand_date;

<强> DEMO

答案 1 :(得分:1)

你可以用mt_rand()这样做:

echo date('Y-m-d')." ".mt_rand(0, 23).":".mt_rand(0, 59).":".mt_rand(0, 59);

答案 2 :(得分:0)

您可以将rand用于此目的

$int= rand(1262055681,1262055681);

您也可以使用mt_rand

$int= mt_rand(1262055681,1262055681); which provides better randomness in the results

请参阅here以获取参考资料

相关问题