faqts : Computers : Programming : Languages : PHP : Function Libraries : Date and Time

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

92 of 155 people (59%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

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+date%2Fdatetime+columns+in+MySQL%3F
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 --  
  MYSQL: 
    o Take a look at the following functions: 
      o PERIOD_DIFF
      o DATE_ADD
      o DATE_SUB 
    o From: 
      http://www.mysql.com/doc/en/Date_and_time_functions.html
  MSACCESS: 
    o DATEDIFF
  POSTGRESQL: 
    o No functions provided, you can use + and - operators. 
    o More detailed info about this -- 
      http://techdocs.postgresql.org/techdocs/faqdatesintervals.php
  SQL SERVER: 
    o Code sample: 
      ldDate1=CTOD(12/01/2002)
      ldDate2=CTOD(02/15/2003) ? (YEAR(ldDate2)+MONTH(ldDate2)/;
      12-YEAR(ldDate1)-MONTH(ldDate1)/12)*12 
  ORACLE: 
    o An interesting article on INTERVALs: http://snipurl.com/171l
    o A readymade procedure: http://snipurl.com/171m
  MS EXCEL: 
    o http://support.microsoft.com/?kbid=214134
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);
?>
NOTES
1. The two dates are inside the mktime functions in the first line. 
Just
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 ;
// or
$sec_diff_2   = $date_end - $date_start ;
//if the two sec_diffs are the same, the calculations are accurate to 
a second.
Nick, www.plumdigitalmedia.com
http://www.tantofa.com
http://www.fantofa.com
http://www.mantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.templatestemp.com