Home » Programming » PHP » Display the total number of days between two dates in PHP

Display the total number of days between two dates in PHP


To count the total number of days between two dates, use the DateTime class and compare them via diff() method with the following few lines of code

$date1 = new DateTime('2020-01-01'); 
$date2 = new DateTime('2020-02-15'); 
print_r($date1->diff($date2));

And the output will be:

DateInterval Object ( 
  [y] => 0 
  [m] => 1 
  [d] => 14 
  [h] => 0 
  [i] => 0 
  [s] => 0 
  [f] => 0 
  [weekday] => 0 
  [weekday_behavior] => 0 
  [first_last_day_of] => 0 
  [invert] => 0 
  [days] => 45 
  [special_type] => 0 
  [special_amount] => 0 
  [have_weekday_relative] => 0 
  [have_special_relative] => 0 
)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.