How can I compute a diference between two datetime columns (in hours) ?
How do I compute / calculate differences between two dates?
How do I calculate the number of years, months, weeks, minutes, seconds between two dates in PHP?
How do I calculate the number of hours between two dates in PHP?
How do I calculate differences between two dates in MS SQL Server?
How do I calculate differences between two dates in Oracle?
Mar 19th, 2008 22:06
ha mo, Nick Nettleton, Shashank Tripathi, Daniel Balan,
This is quite a common question. I am including the ways to calculate
date differences in several databases as well as in PHP. The logic for
PHP should be applicable to many programming languages unless they
already provide handy functions in-built.
However, if you intend to perform these calculations on values from
database columns, then you should consider calculating within the
database itself as that may be the fastest and most efficient way of
doing it. Some examples from popular databases --
o Take a look at the following functions:
o No functions provided, you can use + and - operators.
o More detailed info about this --
o Code sample:
ldDate2=CTOD(02/15/2003) ? (YEAR(ldDate2)+MONTH(ldDate2)/;
o An interesting article on INTERVALs: http://snipurl.com/171l
o A readymade procedure: http://snipurl.com/171m
Anyway, in PHP, the following code sample should help
$dateDiff = mktime(12,0,0,04,20,2003) - mktime(11,0,0,04,20,2003);
echo 'Difference in seconds: ' . $dateDiff . '<br />';
echo '<br />Years Difference = '. floor($dateDiff/365/60/60/24);
echo '<br />Months Difference = '. floor($dateDiff/60/60/24/7/4);
echo '<br />Weeks Difference = '. floor($dateDiff/60/60/24/7);
echo '<br />Days Difference = '. floor($dateDiff/60/60/24);
echo '<br />Hours Difference = '. floor($dateDiff/60/60);
echo '<br />Minutes Difference = '. floor($dateDiff/60);
1. The two dates are inside the mktime functions in the first line.
for your reference, format of mktime function is as follows:
mktime(HOURS, MINUTES, SECONDS, MONTH, DAY, YEAR)
2. Months difference may not be totally accurate as my code above
approximates 4 weeks in a month (think of February etc). However, it
should suffice for all practical purposes, try it.
3. For all the differences, if you want precise numbers instead of
rounded integers, please remove the "floor()" functions on the
calculations. In general though, rounded numbers are what you are
perhaps looking for.
4. The logic of doing this should be fairly similar in many languages
(if the language does not already provide some handy functions to
calculate date differences). The logic is basically to take the
differences between two "epoch times" (no. of seconds from Jan 1 1970)
and divide them by no. of seconds in each year, each month etc etc.
This assumes all months are 28 days, and all years are 365 days. If
you need more accuracy, this is accurate to the second:
// 1 June 2003
$date_start = mktime(0,0,0,04,01,2004);
// 1 July 2004
$date_end = mktime(0,0,0,04,01,2005);
$year_diff = date('y', $date_end) - date('y', $date_start) ;
$month_diff = date('m', $date_end) - date('m', $date_start) +
($year_diff * 12) ;
$week_diff = date('W', $date_end) - date('W', $date_start) +
($year_diff * 52) ; // from PHP 4.1
$day_diff = date('z', $date_end) - date('z', $date_start) + (((int)
date('L', $date_start)) ? ($year_diff * 366) : ($year_diff * 365) ) ;
$hour_diff = $day_diff * 24 ;
$min_diff = $hour_diff * 60 ;
$sec_diff = $min_diff * 60 ;
$sec_diff_2 = $date_end - $date_start ;
//if the two sec_diffs are the same, the calculations are accurate to