php - How can I get X dates in a range with a minimum interval of 1 minute? -


i want random date , time values based on given date range.

ex: start date: 2015-04-29 08:00:00 , end date: 2015-04-29 20:00:00. want 15 random date , time values , should in minimum 1 minute's interval.

i tried 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 can see in above result have highlighted few entries in there distance based on seconds, need minimum of 1 minute's distance in date , time values.

how can code works, there minimum interval of 1 minute between each datetimes ?

this should work you:

here create dateperiod dateinterval of 1 minute. after loop through $period , save dates array. after shuffle() array , take 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 $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 don't want static interval of 1 minute, still want minimum of 1 minute in between each date, can use this:

(here set interval 1 second , filter array way, @ least 1 minute 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 $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);   ?> 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -