Archive

Posts Tagged ‘dateinterval’

Difference in days between two dates [PHP]

January 12, 2011 Leave a comment

Problem

You have two dates and you want to know the difference between them in days.

Solution

$datetime1 = new DateTime('20110107');
# ... = new DateTime('2011-01-07'); is also good format
$datetime2 = new DateTime('20110110');
$interval = $datetime1->diff($datetime2);
print $interval->days;   # 3

Check out the DateTime class, DateTime::diff, and the DateInterval class.

Credits

I found this elegant solution in this thread.

Homework

How old are you (in days)?

Categories: php Tags: , , , ,
Follow

Get every new post delivered to your Inbox.

Join 42 other followers