Solved

How to handle dates < 1970 and > 2037?

Posted on 2003-10-31
9
544 Views
Last Modified: 2012-08-13
Hi,

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.
0
Comment
Question by:keewooi
  • 4
  • 3
  • 2
9 Comments
 
LVL 14

Accepted Solution

by:
ThG earned 125 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
0
 

Author Comment

by:keewooi
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?

Thanx.
0
 
LVL 11

Expert Comment

by:Zontar
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.
0
 
LVL 14

Expert Comment

by:ThG
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)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:keewooi
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.
0
 

Author Comment

by:keewooi
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.
0
 
LVL 11

Expert Comment

by:Zontar
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 ?
0
 
LVL 14

Expert Comment

by:ThG
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.
0
 

Author Comment

by:keewooi
ID: 9685123
Zontar,

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. :)


ThG,

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
   else
         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 .. :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now