How to Get Number of Days Between Two Dates in PHP

Are you want to calculate the difference between two dates in PHP? There are a simple way to get the number of days between two dates in PHP. Using the date_diff() function, you can easily count days between two dates in PHP.

How to Get Number of Days Between Two Dates in PHP

Are you want to calculate the difference between two dates in PHP? There are a simple way to get the number of days between two dates in PHP. Using the date_diff() function, you can easily count days between two dates in PHP.

The following script calculates the difference between two dates using PHP and shows the days count.

<?php
$date1 = date_create("2017-04-15");
$date2 = date_create("2017-05-18");

//difference between two dates
$diff = date_diff($date1,$date2);

//count days
echo 'Days Count - '.$diff->format("%a");
?>

Two PHP functions are used in the above script to get the number of days between two dates.

  • date_create() – This function returns a new DateTime object.
  • date_diff() – This function returns the difference between two DateTime objects.