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 ?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
user1181940
  • 169
  • 1
  • 1
  • 14
  • So where are we with the question? – Rizier123 Apr 29 '15 at 08:39
  • I don't have the time to actually implement it, but what about the following: Don't get all random values at once. I'd store the values in an array. As long as that array does have < 15 entries, get another random value. If that new value meets the condition (at least one minute away from every value we've found so far), write it to the array. If it does not meet the condition, don't write it to the array ;) – Henning Kockerbeck Apr 29 '15 at 08:57

1 Answers1

1

This should work for you:

Here I create a DatePeriod with a DateInterval of 1 minute. After this I loop through $period and save the dates into an array. After this I shuffle() the array and take an array_slice() of 15 elements.

<?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);

?>

possible output:

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
)

EDIT:

If you don't want a static interval of 1 minute, but you still want a minimum of 1 minute in between each date, you can use this:

(Here I just set the interval to 1 second and then filter the array that way, that at least 1 minute is in between each element)

<?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);

 ?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • No offence to @Rizier123, but I'm not sure computing every possible solution and then choosing a few of them randomly is very efficient. I'll add a comment to the question that sketches out a way I think may be more efficient. – Henning Kockerbeck Apr 29 '15 at 08:54