Calculating difference between Two date

Calculating difference between Two date

Most of the time in programming we come across finding difference between dates. In PHP we have no direct function to find difference between two dates there are many mode we can find it , one of the simpler method is subtracting the two time stamp and calculating the difference and dividing it by 86400 (60*60*24 = 1 day).

If both the date are timestamp then we can directly calculate it as

<?php

$date1 = 1130120037;

$date2 = time(); /// current time

$diff = $date2 - $date1;

$date_diff = $diff/(60*60*24)

echo $date_diff;

?>

if the dates are in string format then we have to convert it into time stamp using strtotime and
do the above process.

<?php

$date1 =’1-2-2007′;

$date2 = 7-2-2007;

$diff = strtotime($date2) - strtotime($date1);

$date_diff = $diff/(60*60*24)

echo $date_diff;

?>

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.