[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

timestamp pre 1970 date problem

Posted on 2005-05-10
6
Medium Priority
?
293 Views
Last Modified: 2008-02-01
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));
?>
0
Comment
Question by:rafaelri
5 Comments
 
LVL 6

Expert Comment

by:merwetta1
ID: 13973635
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 13974558
Yeah, the timestamp will be negative but the date() shouldn't have a problem.
0
 

Author Comment

by:rafaelri
ID: 13976446
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
 
LVL 6

Accepted Solution

by:
merwetta1 earned 500 total points
ID: 13981052
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
 
LVL 14

Expert Comment

by:huji
ID: 16214130
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question