timestamp pre 1970 date problem

I've came across a problem, I don't know how to display php timestamps before 1970. There's no problem with the timestamp at all since I'm running a Linux box, but when date function tries to convert it really screws up everything.
Is there any simple way to display pre 70 timestamp dates in php?

Sample:
<?
$timestamp = mktime(0, 0, 0, 3, 28, 1950, 0);
var_dump(date('d-m-y', $timestamp));
?>
rafaelriAsked:
Who is Participating?
 
merwetta1Connect With a Mentor Commented:
from http://php.net/strftime:
"Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch."

From the same page there is a workaround function which I've modified and reprinted here. For the workaround you will use a modified strftime function and you will need to use the date formattings for strftime.

Alternatively, you could look into the ADOdb Date Time Library which is a class for doing exactly this.
http://phplens.com/phpeverywhere/adodb_date_library

Maybe a few more points for this question?

Here is an example of the modified strftime:

<?php

$timestamp = mktime(0, 0, 0, 3, 28, 1950, 0);

echo strftime2('%m-%d-%Y', $timestamp);


function strftime2($format = "", $timestamp = false)
 {
    if ($timestamp >= 0 && $timestamp <= 2147480047)
    { // between 1 Jan 1970 00:00:00 and 19 Jan 2038 03:14:07 GMT
         return strftime ($format, $timestamp);
    }

     $mappings = Array (
        Array ("start" => 1902, "end" => 1951, "map" => 1986), // 1902-1951 = 1986-2035
         Array ("start" => 1952, "end" => 1969, "map" => 1980), // 1952-1969 = 1980-1997
     );
   
    if ($timestamp < 0)
    {
         $year = 1970;
        while ($timestamp < 0)
        {
             $days = ($year % 4 == 0 && ($year % 100 > 0 || $year % 400 == 0)) ? 366 : 365;
             $timestamp += $days * 86400;
             $year--;
        }
    }
    else // if ($timestamp > 2147480047)
     {
         $year = 2038;
        while ($timestamp > 0)
        {
             $days = ($year % 4 == 0 && ($year % 100 > 0 || $year % 400 == 0)) ? 366 : 365;
             $timestamp -= $days * 86400;
             $year++;
        }
    }

    foreach ($mappings as $mapping)
        if ($year >= $mapping["start"] && $year <= $mapping["end"])
        {
             $find = Array ("%y", "%Y", "%D");
             $replace = Array (substr ($year, -2), $year, "%m/%d/" . substr ($year, -2));
             $format =& str_replace ($find, $replace, $format);
             $map_from_1970 = mktime (0, 0, 0, 1, 1, $mapping["map"]);
            return strftime ($format, $timestamp + $map_from_1970);
        }
   
    return "strftime2 (): Year not mapped yet: " . $year;
 }
 
?>

0
 
merwetta1Commented:
when I run your sample code, I get:
string(8) "28-03-50"

That looks OK to me. What are you getting? Perhaps it is a PHP or server configuration issue. What is your PHP version and webserver OS?

according to http://php.net/date :
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

So you should be OK all the way back to 1901. The issue doesn't seem to be the date() function.
0
 
ZylochCommented:
Yeah, the timestamp will be negative but the date() shouldn't have a problem.
0
 
rafaelriAuthor Commented:
The box is a Debian Linux Unstable Running Kernel 2.6.10, PHP 4 and Apache2.
If I var_dump the timestamp, it is fine (has a negative value), the problem seems to occur when I try to convert it to a date, it just displays 31-12-69 when it should be 28-03-50.
Do you know if there's some configuration that could change that behaviour?
0
 
hujiCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: merwetta1

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.