Generating a list of dates with PHP

in #php3 years ago

Ever needed an easy way to generate an array of dates? This is a PHP function which takes two dates as input, and returns an array off all dates between them, inclusive. The input dates are in the format YYYY-MM-DD, as are the dates generated. The validity of input dates are not checked in the function because these were validated in the calling code, and checked after user input.

function array_date_range($strDateFrom,$strDateTo) 
{
    $date_range_array=array();

  $input_from=mktime(1,0,0,substr($strDateFrom,5,2),
  substr($strDateFrom,8,2),substr($strDateFrom,0,4));
  $input_to=mktime(1,0,0,substr($strDateTo,5,2),
  substr($strDateTo,8,2),substr($strDateTo,0,4));

  if ($input_to>=$input_from)
  {
        array_push($date_range_array,date('Y-m-d',$input_from));  //First entry

        while ($input_from<$input_to)
        {
            $input_from+=86400;  add 24 hours
            array_push($date_range_array,date('Y-m-d',$input_from));
        }
  }
  return $date_range_array;
}

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 63851.10
ETH 3059.36
USDT 1.00
SBD 3.85