Menu

How to calculate the difference between two dates using PHP?

Written by

Suppose you have two dates for the form:

Start Date: 2005-03-24
End Date: 2008-06-26

 

I suggest using DateTime and Date Interval objects.

$date1= new DateTime("2005-03-24"); 
$date2 = new DateTime("2008-06-26"); 
$interval = $date1->diff($date2); 
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; 
// shows the total amount of days (not divided into years, months and days like above) 
echo "difference " . $interval->days . " days ";

 

Article Categories:
PHP · Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

Shares