Random time and date between two date values

I am trying to write a php script (or line of code) to repeat a random time and date between two dates, e.g.

2012-12-24 13:03

which would be between my selected dates from October 1, 2012 and January 1, 2013.

Any ideas how best to do this? Thanks in advance.

+4
source share
6 answers

Simple :) Just select 2 random dates, convert to EPOCH and randomly between these two values ​​:)

EPOCH - Time since 01/01/1970, in seconds.
You can use the strtotime() function to make date strings turn into an era, and date() to do it the other way around.

 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('Ymd H:i:s', $rand_epoch); } 
+21
source

You probably want to determine the resolution, for example, one minute or three minutes or 15 seconds or a half days and what not. Randomness should apply for the entire period, I chose one minute here for exemplary purposes (there are 132480 minutes in your period).

 $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]; 

This, for example, gives:

 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" } 

You can find RandomIterator here . Without it, a little more is needed (about 1.5 iterations compared to the above example), using:

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

I also tried with seconds, but it will take quite a while. You probably want to find a random day (92 days) first, and then some random time in it.

I also performed some tests, and I could not find any use when using DatePeriod while you are using general permissions like seconds:

 $start = new Datetime('1st October 2012'); $end = new Datetime('1st Jan 2013'); $random = new DateTime('@' . mt_rand($start->getTimestamp(), $end->getTimestamp())); 

or minutes:

 /** * @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); 
+4
source

Assuming you want to include October 1, but not include January 1 ...

 $start = strtotime("2012-10-01 00:00:00"); $end = strtotime("2012-12-31 23:59:59"); $randomDate = date("Ymd H:i:s", rand($start, $end)); echo $randomDate; 
+2
source

so crazy that it can only be worK

 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('Ymd H:i:s', $rand_date); } 
+2
source

Here is the code for this:

 $randDate=date('Ym-d', mt_rand(strtotime('2012-10-01'), strtotime('2013-01-01'))); 
+1
source

Ok, here is something

 $date_start = strtotime('1 October 2012'); $date_end = strtotime('1 January 2013'); $rand_date = rand($date_start, $date_end); echo(date('dmY H:i', $rand_date)); 
0
source

All Articles