Home > php > Difference in days between two dates [PHP]

Difference in days between two dates [PHP]

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: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: