Faqts : Computers : Programming : Languages : PHP : Function Libraries

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

44 of 51 people (86%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How do I calculate the difference between two dates to return the number of days?

Jun 16th, 2000 07:08
Marco Steinacher, Robert Brown, http://www.php.net/manual/html/function.mktime.html


You can do this with the following statement:
$diff=abs(mktime(0,0,0,$month1,$day1,$year1)-mktime(0,0,0,$month2,$day2,
$year2))/(86400);
Explication:
************
mktime() returns seconds since 1.1.1970 for the specified date.
Thus the difference mktime([date1])-mktime([date2]) tells us the 
seconds between [date1] and [date2].
If we divide the absolute value of this difference by the number of 
seconds per day (24*60*60=86400) we get the number of days between 
[date1] and [date2].
Example:
********
We want to know the difference between 31.12.1999 and 01.03.2000.
The following code tells us the answer:
<?
   $diff=abs(mktime(0,0,0,12,31,1999)-mktime(0,0,0,3,1,2000))/(86400);
   echo $diff;
?>
The script-output is: 61
That is correct then:
  1 day  (31.12.99)
+31 days (in Jan.00)
+29 days (in Feb.00)
---------
=61 days
=========