Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. function rand_date($min_date, $max_date) {
  2. /* Gets 2 dates as string, earlier and later date.
  3. Returns date in between them.
  4. */
  5.  
  6. $min_epoch = strtotime($min_date);
  7. $max_epoch = strtotime($max_date);
  8.  
  9. $rand_epoch = rand($min_epoch, $max_epoch);
  10.  
  11. return date('Y-m-d H:i:s', $rand_epoch);
  12. }
  13.  
  14. $start = new Datetime('1st October 2012');
  15. $end = new Datetime('1st Jan 2013');
  16. $interval = new DateInterval('PT1M'); // Resolution: 1 Minute
  17. $period = new DatePeriod($start, $interval, $end);
  18. $random = new RandomIterator($period);
  19.  
  20. list($result) = iterator_to_array($random, false) ? : [null];
  21.  
  22. class DateTime#7 (3) {
  23. public $date =>
  24. string(19) "2012-10-16 02:06:00"
  25. public $timezone_type =>
  26. int(3)
  27. public $timezone =>
  28. string(13) "Europe/Berlin"
  29. }
  30.  
  31. $count = iterator_count($period);
  32. $random = rand(1, $count);
  33.  
  34. $limited = new LimitIterator(new IteratorIterator($period), $random - 1, 1);
  35. $limited->rewind();
  36. $result = $limited->current();
  37.  
  38. $start = new Datetime('1st October 2012');
  39. $end = new Datetime('1st Jan 2013');
  40.  
  41. $random = new DateTime('@' . mt_rand($start->getTimestamp(), $end->getTimestamp()));
  42.  
  43. /**
  44. * @param DateTime $start
  45. * @param DateTime $end
  46. * @param int|DateInterval $resolution in Seconds or as DateInterval
  47. * @return DateTime
  48. */
  49. $randomTime = function (DateTime $start, DateTime $end, $resolution = 1) {
  50.  
  51. if ($resolution instanceof DateInterval) {
  52. $interval = $resolution;
  53. $resolution = ($interval->m * 2.62974e6 + $interval->d) * 86400 + $interval->h * 60 + $interval->s;
  54. }
  55.  
  56. $startValue = floor($start->getTimestamp() / $resolution);
  57. $endValue = ceil($end->getTimestamp() / $resolution);
  58. $random = mt_rand($startValue, $endValue) * $resolution;
  59.  
  60. return new DateTime('@' . $random);
  61. };
  62.  
  63. $random = $randomTime($start, $end, 60);
  64.  
  65. function randomDate($start_date, $end_date)
  66. {
  67. //make timetamps
  68. $min = strtotime($start_date);
  69. $max = strtotime($end_date);
  70.  
  71. //random date
  72. $rand_date = rand($min, $max);
  73.  
  74. //format it
  75. return date('Y-m-d H:i:s', $rand_date);
  76. }
  77.  
  78. $start = strtotime("2012-10-01 00:00:00");
  79. $end = strtotime("2012-12-31 23:59:59");
  80.  
  81. $randomDate = date("Y-m-d H:i:s", rand($start, $end));
  82.  
  83. echo $randomDate;
  84.  
  85. $date_start = strtotime('1 October 2012');
  86. $date_end = strtotime('1 January 2013');
  87. $rand_date = rand($date_start, $date_end);
  88. echo(date('d.m.Y H:i', $rand_date));
  89.  
  90. $randDate=date('Y-m-d', mt_rand(strtotime('2012-10-01'), strtotime('2013-01-01')));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement