Monday, 2 February 2015

Date diff ( difference) - PHP

Date diff ( difference)

We can get difference of two dates by using diff function. Note that this function is supported by PHP 5.3 and higher versions.Here is an example of knowing the difference in two dates , the first date is taken as present date and past date is created by using date object.

<?Php
date_default_timezone_set ("Asia/Kolkata");
$date1=new DateTime("now") ;
echo $date1->format('d-m-y H:i:s');
echo "<br><br>";
$date2=new DateTime("1976-5-5");
$date= $date1->diff($date2);
echo $date->format('%y Years, %m Months, %d Days');
?>

The output is here

05-03-12 19:17:19
35 Years, 10 Months, 0 Days

0 comments:

Post a Comment