Bug #17123: mktime(), localtime(), strtotime(), etc. are broken for dates <1970 in Win32

From: Date: Thu, 09 May 2002 16:04:12 +0000
Subject: Bug #17123: mktime(), localtime(), strtotime(), etc. are broken for dates <1970 in Win32
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-7335@lists.php.net to get a copy of this message
From: jasonh@trurocollege.ac.uk Operating system: WinNT5.0 PHP version: 4.2.0 PHP Bug Type: Date/time related Bug description: mktime(), localtime(), strtotime(), etc. are broken for dates <1970 in Win32 We've struggled with the fact that mktime(), strtotime(), localtime(), etc. are broken for dates <1970 under Win32. Below is some PHP code that shows how this might be worked around to generate (and use) negative Unix timestamps under Windows. It would be nice if this idea could be incorporated into all the timestamp functions (since we don't want to have to rewrite something like strtotime() which is quite essential for us in deciphering MSSQL datetime fields). The concept is to fiddle dates before 1970 to be dates 56 years later when supplying them to the broken Win32 native functions, and then fiddle the results back to the correct values. This will ensure that leap year counts are still correct and give dates on the correct days of the week. The code below shows fiddling the generation of a full timestamp (mktime()) and fiddling the interpretation of those full timestamps (localtime()). --------- $epoch2 = mktime(0,0,0,1,1,2026); function mymktime($hr,$mi,$sc,$m,$d,$y) { global $epoch2; $fiddle = 0; if($y <= 1970) { // Should be just < 1970, but this will help cope with 0 month or 0 day $fiddle = $epoch2; $y = $y + 56; } return mktime($hr,$mi,$sc,$m,$d,$y) - $fiddle; } function mylocaltime($ts,$assoc) { global $epoch2; $fiddle = false; if($ts < 0 && $ts != -1) { $fiddle = true; $ts += $epoch2; } $res = localtime($ts,$assoc); if($fiddle) $res[$assoc?"tm_year":5] -= 56; return $res; } -- Edit bug report at http://bugs.php.net/?id=17123&edit=1 -- Fixed in CVS: http://bugs.php.net/fix.php?id=17123&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=17123&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=17123&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=17123&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=17123&r=support Expected behavior: http://bugs.php.net/fix.php?id=17123&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=17123&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=17123&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=17123&r=globals

« previous php.bugs (#7335) next »