2个日期值之间的随机时间和日期

时间:2013-01-06 21:12:44

标签: php

我正在尝试编写一个php脚本(或代码行)来回显两个日期之间的随机时间和日期,例如

2012-12-24 13:03

将在我选择的2012年10月1日至2013年1月1日之间。

任何想法如何最好地做到这一点?提前谢谢。

6 个答案:

答案 0 :(得分:25)

简单:)只需选择2个随机日期,转换为EPOCH,并在这两个值之间随机选择:)

EPOCH - 自1970年1月1日以来的时间,以秒为单位 您可以使用strtotime()函数将日期字符串转换为纪元时间,使用date()函数将其作为另一种方式。

function rand_date($min_date, $max_date) {
    /* Gets 2 dates as string, earlier and later date.
       Returns date in between them.
    */

    $min_epoch = strtotime($min_date);
    $max_epoch = strtotime($max_date);

    $rand_epoch = rand($min_epoch, $max_epoch);

    return date('Y-m-d H:i:s', $rand_epoch);
}

答案 1 :(得分:4)

你可能想要定义一个分辨率,例如一分钟,或三分钟或15秒或一天半或不是。随机性应该适用于整个时期,我在这里选择一分钟作为示例目的(你的期间有132480分钟)。

$start    = new Datetime('1st October 2012');
$end      = new Datetime('1st Jan 2013');
$interval = new DateInterval('PT1M'); // Resolution: 1 Minute
$period   = new DatePeriod($start, $interval, $end);
$random   = new RandomIterator($period);

list($result) = iterator_to_array($random, false) ? : [null];    

这例如给出:

class DateTime#7 (3) {
  public $date =>
  string(19) "2012-10-16 02:06:00"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(13) "Europe/Berlin"
}

你可以find the RandomIterator here。没有它,使用以下内容会花费更长的时间(与上面的例子相比,迭代次数约为1.5):

$count    = iterator_count($period);
$random   = rand(1, $count);

$limited = new LimitIterator(new IteratorIterator($period), $random - 1, 1);
$limited->rewind();
$result = $limited->current();

我也尝试了几秒钟,但这需要很长时间。您可能希望首先找到一个随机日(92天),然后在其中找到一些随机时间。

此外,我已经进行了一些测试,到目前为止,使用DatePeriod时我找不到任何好处,只要您使用常见的分辨率,例如秒:

$start    = new Datetime('1st October 2012');
$end      = new Datetime('1st Jan 2013');

$random   = new DateTime('@' . mt_rand($start->getTimestamp(), $end->getTimestamp()));

或分钟:

/**
 * @param DateTime $start
 * @param DateTime $end
 * @param int|DateInterval $resolution in Seconds or as DateInterval
 * @return DateTime
 */
$randomTime = function (DateTime $start, DateTime $end, $resolution = 1) {

    if ($resolution instanceof DateInterval) {
        $interval   = $resolution;
        $resolution = ($interval->m * 2.62974e6 + $interval->d) * 86400 + $interval->h * 60 + $interval->s;
    }

    $startValue = floor($start->getTimestamp() / $resolution);
    $endValue   = ceil($end->getTimestamp() / $resolution);
    $random     = mt_rand($startValue, $endValue) * $resolution;

    return new DateTime('@' . $random);
};

$random = $randomTime($start, $end, 60);

答案 2 :(得分:2)

假设你想包括10月1日,但不包括1月1日......

$start = strtotime("2012-10-01 00:00:00");
$end =  strtotime("2012-12-31 23:59:59");

$randomDate = date("Y-m-d H:i:s", rand($start, $end));

echo $randomDate;

答案 3 :(得分:2)

太疯狂了,可能只是担心

function randomDate($start_date, $end_date)
{
//make timetamps
$min = strtotime($start_date);
$max = strtotime($end_date);

//random date
$rand_date = rand($min, $max);

//format it
return date('Y-m-d H:i:s', $rand_date);
}

答案 4 :(得分:1)

以下是完成此操作的一些代码:

$randDate=date('Y-m-d', mt_rand(strtotime('2012-10-01'), strtotime('2013-01-01')));

答案 5 :(得分:0)

好的,这是

$date_start = strtotime('1 October 2012');
$date_end = strtotime('1 January 2013');
$rand_date = rand($date_start, $date_end);
echo(date('d.m.Y H:i', $rand_date));
相关问题