5

Is there an easy way to get a list of days between two dates in PHP?

I would like to have something like this in the end:

(pseudocode)

date1 = 29/08/2013
date2 = 03/09/2013

resultArray = functionReturnDates(date1, date2);

and the resulting array would contain:

resultArray[0] = 29/08/2013
resultArray[1] = 30/08/2013
resultArray[2] = 31/08/2013
resultArray[3] = 01/09/2013
resultArray[4] = 02/09/2013
resultArray[5] = 03/09/2013

for example.

Juicy
  • 11,840
  • 35
  • 123
  • 212
  • 4
    How about: http://www.php.net/manual/en/class.dateperiod.php ? – Mirco Widmer Aug 29 '13 at 14:25
  • I have tried taking apart the dates and seperate them into date1Day, date1Month, date1Year, date2Day etc... I started by subtracting the different years, months and days to count how many days there were. Then starting from date1 just adding dates. I run into a problem because of months with 30/31/28/29 days... – Juicy Aug 29 '13 at 14:28

5 Answers5

24
$date1 = '29/08/2013';
$date2 = '03/09/2013';

function returnDates($fromdate, $todate) {
    $fromdate = \DateTime::createFromFormat('d/m/Y', $fromdate);
    $todate = \DateTime::createFromFormat('d/m/Y', $todate);
    return new \DatePeriod(
        $fromdate,
        new \DateInterval('P1D'),
        $todate->modify('+1 day')
    );
}

$datePeriod = returnDates($date1, $date2);
foreach($datePeriod as $date) {
    echo $date->format('d/m/Y'), PHP_EOL;
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thank you, this seems to be exactly what I'm looking for. Since I'm quite new to PHP if you have another second could you tell me what the backslashes mean? – Juicy Aug 29 '13 at 14:38
  • 1
    It's a good idea to learn about DateTime objects: powerful and flexible, timezone and daylight savings aware; they and their related objects (DatePeriod and DateInterval) can really make working with dates simple – Mark Baker Aug 29 '13 at 14:41
  • @user2018084 see [DateTime](http://www.php.net/manual/en/class.datetime.php) – IROEGBU Aug 29 '13 at 14:54
  • 1
    The backslashes are a [namespace](http://www.php.net/manual/en/language.namespaces.php) reference, indicating classes in the global scope.... if you're not using namespaces, then you don't need to worry about them: they're not needed, but they won't do any harm – Mark Baker Aug 29 '13 at 15:48
7
function DatePeriod_start_end($begin,$end){

        $begin = new DateTime($begin);

        $end = new DateTime($end.' +1 day');

        $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);

        foreach($daterange as $date){
            $dates[] = $date->format("Y-m-d");
        }
        return $dates;

    }
Chirag Pipariya
  • 445
  • 5
  • 13
2

dunno if this is at all practical, but it works pretty straight-forward

$end = '2013-08-29';
$start = '2013-08-25';
$datediff = strtotime($end) - strtotime($start);
$datediff = floor($datediff/(60*60*24));
for($i = 0; $i < $datediff + 1; $i++){
    echo date("Y-m-d", strtotime($start . ' + ' . $i . 'day')) . "<br>";
}
robz228
  • 630
  • 1
  • 4
  • 11
0

Try this:

function daysBetween($start, $end)
   $dates = array();
   while($start <= $end)
   {
       array_push(
           $dates,
           date(
            'dS M Y',
            $start
           )
       );
       $start += 86400;
   }
   return $dates;
}

$start    = strtotime('2009-10-20');
$end    = strtotime('2009-10-25'); 
var_dump(daysBetween($start,$end));
Norbert
  • 302
  • 1
  • 9
  • 19
0
$datearray = array();
   $date = $date1;
   $days = ceil(abs($date2 - $date1) / 86400) + 1;//no of days

   for($i = 1;$i <= $days; $i++){

     array_push($datearray,$date);
     $date = $date+86400;

   }
     foreach($datearray as $days){
      echo date('Y-m-d, $days);
      }
Motolola
  • 368
  • 5
  • 19