×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: fatelord
Added: May 9, 2014 11:32 AM
Modified: Dec 4, 2014 8:47 AM
Views: 26
Tags: array date range
Create a range of days between two dates
  1. function createDateRangeArray($strDateFrom, $strDateTo, $format) {
  2.         // takes two dates formatted as YYYY-MM-DD and creates an
  3.         // inclusive array of the dates between the from and to dates.
  4.         // could test validity of dates here but I'm already doing
  5.         // that in the main script
  6.  
  7.         $aryRange = array();
  8.  
  9.         $iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2), substr($strDateFrom, 8, 2), substr($strDateFrom, 0, 4));
  10.         $iDateTo = mktime(1, 0, 0, substr($strDateTo, 5, 2), substr($strDateTo, 8, 2), substr($strDateTo, 0, 4));
  11.  
  12.         if ($iDateTo >= $iDateFrom) {
  13.             array_push($aryRange, date($format, $iDateFrom)); // first entry
  14.             while ($iDateFrom < $iDateTo) {
  15.                 $iDateFrom+=86400; // add 24 hours
  16.                 array_push($aryRange, date($format, $iDateFrom));
  17.             }
  18.         }
  19.         return $aryRange;
  20.     }