Faqts : Business : Programming : Shopping For You : 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?

96 of 160 people (60%) answered Yes
Recently 9 of 10 people (90%) 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?

Aug 27th, 2009 16:03
khalid khalid, forum net tr, chat alarab, ha mo, Nick Nettleton, Shashank Tripathi, Daniel Balan, http://www.jr7sa.com


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 --  

http://www.jr7sa.com
http://www.jr7sa.com/msn
http://www.jr7sa.com/vb
http://www.jr7sa.com/1
http://www.jr7sa.com/wordpress
http://www.jr7sa.com/nkt
http://www.jr7sa.com/chat
http://www.jr7sa.com/cam
http://www.jr7sa.com/newcaam
http://jr7sa.com/vb/f2.html
http://jr7sa.com/vb/f3.html
http://jr7sa.com/vb/f4.html
http://jr7sa.com/vb/f15.html
http://jr7sa.com/vb/f56.html
http://jr7sa.com/vb/f57.html
http://jr7sa.com/vb/f17.html
http://jr7sa.com/vb/f50.html
http://jr7sa.com/vb/f38.html
http://jr7sa.com/vb/f21.html
http://jr7sa.com/vb/f23.html
http://jr7sa.com/vb/f43.html
http://jr7sa.com/vb/f39.html
http://jr7sa.com/vb/f44.html
http://jr7sa.com/vb/f46.html
http://jr7sa.com/vb/f49.html
http://jr7sa.com/vb/f27.html
http://jr7sa.com/vb/f28.html
http://jr7sa.com/vb/f40.html
http://jr7sa.com/vb/f54.html
http://jr7sa.com/vb/cv_rss_feeds.php
http://www.jr7sa.com/vb/archive/index.php/
http://jr7sa.com/vb/f47.html
http://jr7sa.com/vb/f35.html
http://jr7sa.com/vb/external.php?type=RSS2
http://jr7sa.com/vb/jr7sa.com_vb_sitemap.xml
http://jr7sa.com/vb/sitemap.php

  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.youtubevideolari.net youtube videos
online video tr tube http://www.videotrtube.com
full forum http://www.forumnettr.com/forum 
secilmis youtube videolari 
firmalar sirketler rehberi http://www.sirketfirmarehberi.com firma
sirket rehberi adres telefon bilgileri
full program full oyun http://www.forumnettr.com