Page 1 of 1

mktime function not liking being negative

Posted: Mon May 16, 2005 1:05 pm
by mrvanjohnson
Alright I know that the mktime function on Windows does not support negative time stamps. But my Unix boxes always have until now. I just migrated to a brand new server running PHP Version 4.3.10 on Red Hat and now it appears I have lost the function to work with mktime with a negative value, or basically anything before the date January 1, 1970. Has anyone else seen this problem and are there any good work arounds?

Posted: Sat May 28, 2005 5:29 am
by JAM
Using Slackware and PHP Version 4.3.11 it works (Win32 of course not):

Code: Select all

echo date("M-d-Y", mktime(0, 0, 0, 0, 0, 1970));
Sounds wierd that it shouldn't on your end... :?
By the looks of the usercomment of php.net's mktime page, there seems to be various workaround. I think they are intended for the win32 enviroment, but if it really is broken on your end, why not try...

Update:
You should verify the glibc issue. I just remembered that I've read about this somewhere, and according to the bugreports on php.net...
[4 May 2003 5:58am CEST] derick@php.net

But that's not a bug in PHP, but more in your glibc :) Which version of
glibc do you use? AFAIK the 2.3.x series return an undefined value for
dates smaller than 1 - 1 - 1970, much like Windows does.

[4 May 2003 10:35am CEST] guy dot mersch at pandora dot be

the linux server uses glibc 2.2.5 stable

[4 May 2003 11:44am CEST] guy dot mersch at pandora dot be

There seems to be indeed a problem with glibc 2.2.5 and
the mktime function for dates earlier than 01/01/1970

Posted: Sat May 28, 2005 1:53 pm
by Skara
It doesn't work for me either. I use the following as a replacement mktime(). Got it from a comment on php.net.

Code: Select all

function MakeTime() {
   $objArgs = func_get_args();
   $nCount = count($objArgs);
   if ($nCount < 7)
   {
       $objDate = getdate();
       if ($nCount < 1)
           $objArgs[] = $objDate["hours"];
       if ($nCount < 2)
           $objArgs[] = $objDate["minutes"];
       if ($nCount < 3)
           $objArgs[] = $objDate["seconds"];
       if ($nCount < 4)
           $objArgs[] = $objDate["mon"];
       if ($nCount < 5)
           $objArgs[] = $objDate["mday"];
       if ($nCount < 6)
           $objArgs[] = $objDate["year"];
       if ($nCount < 7)
           $objArgs[] = -1;
   }
   $nYear = $objArgs[5];
   $nOffset = 0;
   if ($nYear < 1970)
   {
       if ($nYear < 1902)
           return 0;
       else if ($nYear < 1952)
       {
           $nOffset = -2650838400;
           $objArgs[5] += 84;
           // Apparently dates before 1942 were never DST
           if ($nYear < 1942)
               $objArgs[6] = 0;
       }
       else
       {
           $nOffset = -883612800;
           $objArgs[5] += 28;
       }
   }

   return call_user_func_array("mktime", $objArgs) + $nOffset;
}