note 8516 added to mktime
| From: | dante at lorenso dot com | Date: | Thu, 14 Sep 2000 20:24:45 +0000 |
| Subject: | note 8516 added to mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-5064@lists.php.net to get a copy of this message | ||
the mktime() function paired with the strftime() function has a nice feature of being able to do
date addition and subtraction!
<p>
If you have a date of 2000-08-01 and you want to know the date if you shift XXX days forward, you
can do the following:
<p>
<code><pre>
$yr = "2000";
$mn = "8";
$dy = "1";
$offset = 54;
$newday = strftime("%Y-%m-%d",
mktime(0,0,0,$mn,$dy+$offset,$yr));
</pre></code>
<p>
This is great! However, there is one thing you may want to take note of...namely INTEGER
limitations. Apparently the mktime() function returns the UNIX time conversion of a date where the
values represent seconds since Jan 1, 1970, and are positive or negative depending on whether the
date appears before or after 1970. Due to the MAX_INT (LONG?) size, dates can only be represented
as far back as 1902 (- MAX) and as far forward as 2037 (+ MAX).
<p>
Also, the addition of any field that might exceed the max_int limitation itself will cause an
invalid calculation and reset the date to Dec 31, 1969 (Unix time of -1).
<p>
<code><pre>
EX:
$day_shift = 36524;
$stmp = mktime(0,0,0,1,$day_shift,1903);
// $stmp == -1 !!
// why ? because
// 36524 days == 3155673600 secs)
</pre></code>
<p>
In this case, the 3155673600 shift is too large for the mktime() function to compute, so you end up
with -1. However, if you do your same shift in smaller increments (say 10,000 days at
a time) the calculations will work and you'll be fine ;)
http://www.php.net/manual/function.mktime.php