Advertisement
neatekFb

Get dates between two dates (PHP)

Jun 14th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. <?php
  2. // Vladimir Zhelnov // neatek.ru // neatek.pw
  3. // get dates between 2 dates. ;)
  4.  
  5. function getArrayDates($date1, $date2, $timestamp = false) {
  6.     $period = new DatePeriod(
  7.          new DateTime(date('Y-m-d', strtotime($date1)) ),
  8.          new DateInterval('P1D'),
  9.          new DateTime(date('Y-m-d', strtotime($date2. "+1 days")))
  10.     );
  11.  
  12.  
  13.     $array=array();
  14.     foreach( iterator_to_array($period) as $date) {
  15.         if(is_string($date->format('Y-m-d'))) {
  16.             if($timestamp)
  17.                 $array[] = (int) strtotime($date->format('Y-m-d'));
  18.             else
  19.                 $array[] = $date->format('Y-m-d');
  20.         }
  21.     }
  22.  
  23.     return $array;
  24. }
  25.  
  26. /*
  27.  
  28. Usage : getArrayDates('2017-06-14', '2017-07-01');
  29.  
  30. Result:
  31. array(18) {
  32.   [0]=>
  33.   string(10) "2017-06-14"
  34.   [1]=>
  35.   string(10) "2017-06-15"
  36.   [2]=>
  37.   string(10) "2017-06-16"
  38.   [3]=>
  39.   string(10) "2017-06-17"
  40.   [4]=>
  41.   string(10) "2017-06-18"
  42.   [5]=>
  43.   string(10) "2017-06-19"
  44.   [6]=>
  45.   string(10) "2017-06-20"
  46.   [7]=>
  47.   string(10) "2017-06-21"
  48.   [8]=>
  49.   string(10) "2017-06-22"
  50.   [9]=>
  51.   string(10) "2017-06-23"
  52.   [10]=>
  53.   string(10) "2017-06-24"
  54.   [11]=>
  55.   string(10) "2017-06-25"
  56.   [12]=>
  57.   string(10) "2017-06-26"
  58.   [13]=>
  59.   string(10) "2017-06-27"
  60.   [14]=>
  61.   string(10) "2017-06-28"
  62.   [15]=>
  63.   string(10) "2017-06-29"
  64.   [16]=>
  65.   string(10) "2017-06-30"
  66.   [17]=>
  67.   string(10) "2017-07-01"
  68. }
  69.  
  70. OR TIMESTAMPS : Usage : getArrayDates('2017-06-14', '2017-07-01', true);
  71.  
  72. array(18) {
  73.   [0]=>
  74.   int(1497398400)
  75.   [1]=>
  76.   int(1497484800)
  77.   [2]=>
  78.   int(1497571200)
  79.   [3]=>
  80.   int(1497657600)
  81.   [4]=>
  82.   int(1497744000)
  83.   [5]=>
  84.   int(1497830400)
  85.   [6]=>
  86.   int(1497916800)
  87.   [7]=>
  88.   int(1498003200)
  89.   [8]=>
  90.   int(1498089600)
  91.   [9]=>
  92.   int(1498176000)
  93.   [10]=>
  94.   int(1498262400)
  95.   [11]=>
  96.   int(1498348800)
  97.   [12]=>
  98.   int(1498435200)
  99.   [13]=>
  100.   int(1498521600)
  101.   [14]=>
  102.   int(1498608000)
  103.   [15]=>
  104.   int(1498694400)
  105.   [16]=>
  106.   int(1498780800)
  107.   [17]=>
  108.   int(1498867200)
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement