1 (edited by Mastodon 2006-04-01 09:30)

Topic: Negative timestamps

Is it just me or does PHP disallow negative timestamps (referring to time before the Unix Epoch) in some functions, like strtotime? When I first started running into this problem, I figured it was my host's PHP installation not allowing negative timestamps whatsoever, but apparently this isn't the case.

On my host, this code prints a nicely formatted, correct date :

echo date("m/d/Y", -884908800);

But this prints -1 on dates previous to January 1, 1970:

$month = 'December';
$day = 17;
$year = 1941;

echo strtotime($month . ' ' . $day . ', ' . $year);

It's really annoying me that I can't work with dates in some functions. What gives?

PHP 4.4.2

Re: Negative timestamps

php.net wrote:

Note:  The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

Re: Negative timestamps

Thanks, Connorhd. The fact that functions could take negative stamps in input but not output was confusing me.

Moving on... is there a simple way to work with dates without using timestamps?

Re: Negative timestamps

Hmm, sorry i don't really have any ideas

Looking at the php.net page again though, other people have

function safestrtotime ($s) {
       $basetime = 0;
       if (preg_match ("/19(\d\d)/", $s, $m) && ($m[1] < 70)) {
               $s = preg_replace ("/19\d\d/", 1900 + $m[1]+68, $s);
               $basetime = 0x80000000 + 1570448;
       }
       return $basetime + strtotime ($s);
}

theres some other alternatives on there too http://uk2.php.net/strtotime (just search the page for negative)

Re: Negative timestamps

Connorhd wrote:

Hmm, sorry i don't really have any ideas

Looking at the php.net page again though, other people have

function safestrtotime ($s) {
       $basetime = 0;
       if (preg_match ("/19(\d\d)/", $s, $m) && ($m[1] < 70)) {
               $s = preg_replace ("/19\d\d/", 1900 + $m[1]+68, $s);
               $basetime = 0x80000000 + 1570448;
       }
       return $basetime + strtotime ($s);
}

theres some other alternatives on there too http://uk2.php.net/strtotime (just search the page for negative)

Thanks, I'll try that.