Solution for : How to calculate the difference between two dates using PHP?
I suggest to use DateTime
and DateInterval
objects.
1 2 3 4 5 6 7 | $date1 = new DateTime( "2007-03-24" ); $date2 = new DateTime( "2009-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 " ; |
read more php DateTime::diff manual
From the manual:
As of PHP 5.2.2, DateTime objects can be compared using comparison operators.
1 2 3 4 5 6 | $date1 = new DateTime( "now" ); $date2 = new DateTime( "tomorrow" ); var_dump( $date1 == $date2 ); // bool(false) var_dump( $date1 < $date2 ); // bool(true) var_dump( $date1 > $date2 ); // bool(false) |
No comments:
Post a Comment