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.
keewooiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Giovanni GCTOCommented:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
keewooiAuthor Commented:
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
ZontarCommented:
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Giovanni GCTOCommented:

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
keewooiAuthor Commented:
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
keewooiAuthor Commented:
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
ZontarCommented:
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
Giovanni GCTOCommented:
> 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
keewooiAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.