DateDiff replacement in PHP
| From: | Carlos E . Guillén | Date: | Mon, 21 Aug 2000 22:35:03 +0000 |
| Subject: | DateDiff replacement in PHP | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-2002@lists.php.net to get a copy of this message | ||
I found this in php.faqts.com, and seems very useful.
Jun 16th, 2000 10:08
Robert Brown, Marco Steinacher
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
=========