Efficient way to find Date Difference in PHP

If you’re using PHP 5.3 >, this is by far the most accurate way of calculating the difference:

$date1 = new DateTime("2010-07-06");
$date2 = new DateTime("2010-07-09");

$diff = $date2->diff($date1)->format("%a");

Or,


The date_diff() function returns the difference between two DateTime objects.

Syntax: date_diff(datetime1,datetime2,absolute); $date1=date_create(“2013-03-15”); $date2=date_create(“2013-12-12”); $diff=date_diff($date1,$date2); echo $diff->format(“%R%a days”);

Advertisements

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 )

Google+ photo

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

Connecting to %s