// you’re reading...

PHP

Get Days Between Two Dates Using PHP

This is a simple snippet of code that will return an array of days between two dates.

Read on for a little more info…

<?php
function GetDays($sStartDate, $sEndDate){
  // Firstly, format the provided dates.
  // This function works best with YYYY-MM-DD
  // but other date formats will work thanks
  // to strtotime().
  $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));
  $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));

  // Start the variable off with the start date
  $aDays[] = $sStartDate;

  // Set a 'temp' variable, sCurrentDate, with
  // the start date - before beginning the loop
  $sCurrentDate = $sStartDate;

  // While the current date is less than the end date
  while($sCurrentDate < $sEndDate){
    // Add a day to the current date
    $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));

    // Add this new day to the aDays array
    $aDays[] = $sCurrentDate;
  }

  // Once the loop has finished, return the
  // array of days.
  return $aDays;
}
?>

Usage:

$aDays = GetDays('5th Feb 2007', '10th Feb 2007');

You can use most date formats, such as:

GetDays('2007-01-01', '2007-01-31');

or

GetDays('19-02-2007', '25-02-2007');

Whatever strtotime can handle, you can pass to this function.

Hope it helps!

Discussion

One comment for “Get Days Between Two Dates Using PHP”

  1. Saint!

    Posted by Ben McDougal | August 18, 2008, 9:53 am

Post a comment

Most Emailed