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.

2
  • 4
    How about: php.net/manual/en/class.dateperiod.php ? Commented Aug 29, 2013 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... Commented Aug 29, 2013 at 14:28

5 Answers 5

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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
@user2018084 see DateTime
The backslashes are a namespace 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
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;

    }

Comments

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>";
}

Comments

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));

Comments

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);
      }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.