PHP Snippet: Get dates from a given range

By | March 8, 2011
function get_range_dates($start, $end){
  $days = array();
  $start_tmp = explode("-",$start);
  $end_tmp = explode("-",$end);
  $start_time = mktime(1,1,1, $start_tmp[1], $start_tmp[2], $start_tmp[0]);
  $end_time = mktime(1,1,1, $end_tmp[1], $end_tmp[2], $end_tmp[0]);       
  while($start_time <= $end_time){           
    $days[] = date("Y-m-d", $start_time);
    $start_time += 86400;
  }
  return $days;
}

//calling the function
get_range_dates('2011-02-01', '2011-03-08');

Result:

Array ( [0] => 2011-02-01 [1] => 2011-02-02 [2] => 2011-02-03 [3] => 2011-02-04 [4] => 2011-02-05 [5] => 2011-02-06 [6] => 2011-02-07 [7] => 2011-02-08 [8] => 2011-02-09 [9] => 2011-02-10 [10] => 2011-02-11 [11] => 2011-02-12 [12] => 2011-02-13 [13] => 2011-02-14 [14] => 2011-02-15 [15] => 2011-02-16 [16] => 2011-02-17 [17] => 2011-02-18 [18] => 2011-02-19 [19] => 2011-02-20 [20] => 2011-02-21 [21] => 2011-02-22 [22] => 2011-02-23 [23] => 2011-02-24 [24] => 2011-02-25 [25] => 2011-02-26 [26] => 2011-02-27 [27] => 2011-02-28 [28] => 2011-03-01 [29] => 2011-03-02 [30] => 2011-03-03 [31] => 2011-03-04 [32] => 2011-03-05 [33] => 2011-03-06 [34] => 2011-03-07 [35] => 2011-03-08 )



Category: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.