Link to home
Start Free TrialLog in
Avatar of keewooi
keewooi

asked on

How to handle dates < 1970 and > 2037?

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.
ASKER CERTIFIED SOLUTION
Avatar of Giovanni G
Giovanni G
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of keewooi
keewooi

ASKER

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

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)
Avatar of keewooi

ASKER

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.
Avatar of keewooi

ASKER

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.
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 ?
> 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.
Avatar of keewooi

ASKER

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