dante@lorenso.com
14-Sep-2000 10:24
the mktime() function paired with the strftime() function has a nice feature of being able to do date addition and subtraction!
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:
$yr = "2000";
$mn = "8";
$dy = "1";
$offset = 54;
$newday = strftime("%Y-%m-%d",
mktime(0,0,0,$mn,$dy+$offset,$yr));
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).
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).
EX:
$day_shift = 36524;
$stmp = mktime(0,0,0,1,$day_shift,1903);
// $stmp == -1 !!
// why ? because
// 36524 days == 3155673600 secs)
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 😉