[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 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