1

I want to get some random date and time values based on a given date range.

Ex: start date: 2015-04-29 08:00:00 and end date: 2015-04-29 20:00:00. Now I want to get the 15 random date and time values and also it should be in minimum 1 minute's interval.

I tried the following code:

rand_date($min_date, $max_date,$total_number); 

function rand_date($min_date, $max_date,$total_number) 
{
    $min_epoch = strtotime($min_date);
    $max_epoch = strtotime($max_date);

    for($i=0;$i<=$total_number;$i++)
    {
          $rand_epoch = rand($min_epoch, $max_epoch);
          echo "in::".date('Y-m-d H:i:s', $rand_epoch)."\n\n";
    }
}

current output:

in::2015-04-29 17:41:13 //<-   
in::2015-04-29 17:41:15 //<-    
in::2015-04-29 15:38:39    
in::2015-04-29 17:41:50 //<-    
in::2015-04-29 17:45:21    
in::2015-04-29 11:50:57    
in::2015-04-29 19:34:12    
in::2015-04-29 14:05:55    
in::2015-04-29 11:25:36    
in::2015-04-29 15:46:53    
in::2015-04-29 14:55:44    
in::2015-04-29 19:53:30   
in::2015-04-29 18:28:03   
in::2015-04-29 08:52:13   
in::2015-04-29 17:59:42

As you can see in the above result I have highlighted a few entries in which there is only distance based on seconds, but actually I need a minimum of 1 minute's distance in all date and time values.

How can I get the code to works, so that there is a minimum interval of 1 minute between each dateTimes ?

4

1 回答 1

1

这应该适合你:

DatePeriod在这里,我用1 分钟创建了一个DateInterval。在此之后,我循环$period并将日期保存到一个数组中。在此之后,我shuffle()数组并取array_slice()15 个元素。

<?php

    $start = new DateTime("2015-04-29 08:00:00");
    $interval = new DateInterval("PT1M");
    $end = (new DateTime("2015-04-29 20:00:00"))->add($interval);

    $period = new DatePeriod($start, $interval, $end);

    foreach($period as $date)
        $dates[] = $date->format("Y-m-d H:i:s");

    shuffle($dates);

    $result = array_slice($dates, 0 ,15);

    print_r($result);

?>

可能的输出:

Array
(
    [0] => 2015-04-29 09:37:00
    [1] => 2015-04-29 14:41:00
    [2] => 2015-04-29 18:30:00
    [3] => 2015-04-29 15:37:00
    [4] => 2015-04-29 17:37:00
    [5] => 2015-04-29 09:18:00
    [6] => 2015-04-29 08:18:00
    [7] => 2015-04-29 10:39:00
    [8] => 2015-04-29 14:15:00
    [9] => 2015-04-29 13:45:00
    [10] => 2015-04-29 13:06:00
    [11] => 2015-04-29 10:04:00
    [12] => 2015-04-29 18:24:00
    [13] => 2015-04-29 13:47:00
    [14] => 2015-04-29 18:15:00
)

编辑:

如果您不想要 1 分钟的静态间隔,但仍希望每个日期之间至少有 1 分钟的间隔,则可以使用以下命令:

(这里我只是将间隔设置为 1 秒,然后以这种方式过滤数组,每个元素之间至少间隔 1 分钟)

<?php

    $start = new DateTime("2015-04-29 08:00:00");
    $interval = new DateInterval("PT1S");
    $end = (new DateTime("2015-04-29 20:00:00"))->add($interval);

    $period = new DatePeriod($start, $interval, $end);

    foreach($period as $date)
        $dates[] = $date->format("Y-m-d H:i:s");

    shuffle($dates);

    $pre = $start->getTimestamp();
    $dates = array_filter($dates, function($v)use(&$pre){
        if(abs(strtotime($v) - $pre) >= 60) {
            $pre = strtotime($v);
            return TRUE;
        } else {
            $pre = strtotime($v);
            return FALSE;       
        }
    });

    $result = array_slice($dates, 0 ,15);

    print_r($result);

 ?>
于 2015-04-29T07:46:19.440 回答