How to handle dates < 1970 and > 2037?

Posted on 2003-10-31
Medium Priority
Last Modified: 2012-08-13

I use PHP's date() function to handle dates in Unix timestamp format. However, the date function has an inherited limitation of supporting only dates from 1970 to 2037 (1900 to 2037 if in non-windows environment).

This posses a serious limitation to the language. A range of 1970 to 2037 is simply too short to store dates. For instance, you can't use it to store dates of birth. And by using it store dates before 1970 (by using negative values), you are risking your code for not being able to run in windows platform. PHP codes, afterall are meant to run-one run-all.

So, is there any workaround for this? Such as a date() replacement?

Thank you.
Question by:keewooi
  • 4
  • 3
  • 2
LVL 14

Accepted Solution

ThG earned 375 total points
ID: 9662629

nice question. OK, PHP has no support for handling such dates. So the only way would be to store dates in Y/m/d format...but then there would be the problem for dates calculations etc.
There would be a small library dedicated to that but wait.. A quick search in the PEAR database returned what I expected:

Description:        Generic classes for representation and manipulation of
dates, times and time zones without the need of timestamps,
which is a huge limitation for php programs. Includes time zone data,
time zone conversions and many date/time conversions.
It does not rely on 32-bit system date stamps, so
you can display calendars and compare dates that date
pre 1970 and post 2038. This package also provides a class
to convert date strings between Gregorian and Human calendar formats.

Download from http://pear.php.net/package/Date

Author Comment

ID: 9664932
hi ThG,

Thanx for the suggestion, I am starting to see hope.

I installed the date package, but didn't get it run at the first time, due to a file missing from the PEAR installation. The file is Span.php, and can be downloaded from http://cvs.php.net/cvs.php/pear/Date/Date/Span.php?login=2.

The date package seems promising, but it seriously lack proper documentation. Is there anywhere i can look for one?

LVL 11

Expert Comment

ID: 9665302
MySQL supports dates from the beginning of the year 1000 AD to the end of AD 9999, and has pretty good date-handling funcitons built in. If I need to operate outside the 1970-2037 timeframe, that's what I use.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 14

Expert Comment

ID: 9665786

keewooi: Yes, as most of the PEAR packages, it lacks documentation. Fortunately they are very easy to read so just open the package files and check out how they work (focus on public methods, you don't need to know how internal methods work)

Author Comment

ID: 9676949
I don't want to use the date type to store dates and times, as I want to create an application that does not limit itself to a specific SQL vendor. Correct me if I am wrong, but the DATETIME field type is the type of field that will give you most problem when porting from one RBDMS to another. An INT(10) to store unix timestamps will never go wrong.

Besides, PHP datetime functions only handle unix timestamp.

Author Comment

ID: 9684677
The PEAR Date package is still somehow unsatisfactory. Because I am looking for a way to handle unix timestamp that is < 1970 and > 2037. The package does provide a set of functions to manipulate the dates, but it still goes back to PHP's date and time functions, so the limit still exists.

ThG, i will definitely give you the points for your suggestions. But meanwhile, I don't want to close the topic yet, as I wish to have more suggestions from the others (and yourself, of course).

So, can someone please tell me the solutions to it? My problem is simple:

* I want to create an application that store dates in database (any database).
* I don't want to use the DATETIME type, because I understand that moving the data of DATETIME type from one database to another could be difficult (please correct me if I am wrong).
* A unix timestamp would be just nice. But PHP does not recognize timestamp that is < 1970 and > 2037.
* So, is there any date() function replacement?

Sorry for being such a nuisance.
LVL 11

Expert Comment

ID: 9685016
I wrote a port of the JavaScript/ECMAScript Date class to PHP because, while there's lots of things to like about PHP, its date handling functions are not (IMO) among them.

Unfortunately, the current version of my class uses date() for some of the methods -- I'm rewriting these to be independent of them, but they're not yet ready.

Have you tried www.phpclasses.org ?
LVL 14

Expert Comment

ID: 9685078
> The PEAR Date package is still somehow unsatisfactory. Because I am looking for a way to handle unix timestamp that is < 1970
> and > 2037. The package does provide a set of functions to manipulate the dates, but it still goes back to PHP's date and time
> functions, so the limit still exists.

No, you have no ways to handle them in a serious way. The limit imposed by those two years is the 32bits integer overflow. So there is NO way to handle dates using unix timestamps.
You *have* to use an alternate method like the one used by Date packages.. of course you may reimplement it to better fit your needs, but that's the way it has to work. MySQL also handles them that way.

Author Comment

ID: 9685123

I've tried www.phpclasses.org. Too bad there isn't any that I am looking for.

Good luck for your class rewriting. Would appreciate if you could share when it is ready. :)


Ouch .. sad to hear that. I think I will have to do it this way:
* The database server stores the timestamp in INT(10)
* Write a new date() function to process dates.
   if (date< 1970 || date > 2037)
         use the new date() function
         use the original PHP function
* Only PHP does the datetime translation. Avoid using mysql's at all.

Hopefully I will be able to write that function .. :)

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

862 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