Social Icons

Tuesday, August 27, 2013

Difference between two dates in php

Here, I am going to tell you the solution for if you have to find out the difference between two dates in php. Suppose, you have two dates and you want the interval difference between them in terms of year, month, day, hours, minutes and seconds then you can do it as follows:

<?php
$old_date = '2012-08-26';
$cur_date = date("Y-m-d H:i:s"); //This will give you current date and time

$old_date = new DateTime($old_date);
$cur_date = new DateTime($cur_date);

$interval = $cur_date->diff($old_date);
$year     = $interval->y; //Outputs year difference
$month  = $interval->m; //Outputs month difference
$day      = $interval->d; //Outputs days difference
?>

From the above code, you can easily get the date difference between two dates.

Total Pageviews