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)?

About these ads
Categories: php Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 42 other followers

%d bloggers like this: