php

PHP program to find the number of days between two dates

Trying to get the number of days between two dates? You can use the methods explained in this post.

<?php
    // If you are using PHP 5.3 or above
	$date_1 = new DateTime("2021-09-20");
    $date_2 = new DateTime("2021-10-22");

    $num_of_days = $date_1->diff($date_2)->format("%a");

    echo $num_of_days;
?>

Output

32

Get the number of days between dates using strtotime()

You can also use the strtotime() function to calculate the number of days between two dates. You just need to calculate the difference between the given dates and divide them to (60 * 60 * 24).

Code Example

<?php
  $date_a = strtotime("2021-03-27");
  $date_b = strtotime("2021-04-25");
  
  $diff_in_dates = $date_b - $date_a;
  
  echo round($diff_in_dates / (60 * 60 * 24));
?>

Output

29
Was this helpful?