This is now depricated. Please use the DateTime class for all your PHP date/time needs.
I've consistently had a problem with date validation/formatting in PHP on Linux for as long as I can remember. The root of this problem is a little thing called the Unix Epoch. Basically, the way "time" is perceived on a POSIX (aka unix-like) operating system is not as a "time" as you would see on a digital clock, but instead as a running number counting the seconds since the birth of Unix, which is rounded to Midnight on the morning of January 1st, 1970, known as the Unix Epoch. The number is called a unix timestamp. For example, the current time of this writing, 03-03-2005 02:17:35, is viewed as :
1219629635
Well, validating a time in PHP under Linux has always been as simple as transforming a text date, such as "12/23/1986", into a unix timestamp with the function call:
strtotime("12/23/1986");
If the function is able to convert this into a unix timestamp, then it is a valid date and as such, one can use this timestamp to format the date into any number of date formats with the "date()" function.
A problem arises when you try to validate a date prior to the Unix epoch. Seeing as time doesn't seem to exist to PHP before the Unix epoch, it is impossible to glean a unix timestamp from it, and this function returns "-1", signifying that it isn't a valid date. This is utter horseshit.
The only ways around this I have read are 1) some distros of Linux magically return a workable negative timestamp when given a date prior to the epoch and 2) using some code to just pump it to MySQL, which has actual date verification for it's date fields, seeing if MySQL accepts it, and returning it from MySQL in a formatted fashion.
Neither of these are options for me because a) I'll be damned if I'm going to be tied to a specific distro for this one purpose, and b) I'll be double-damned if I'm going to be tied to a database for this one purpose.
So, what does a guy with these strict principles and a fetish for writing bloated, algorithmic code do? Why of course, he writes his own brute force date validator in PHP and shares it with the world.
Here's a quick code example of how it works:
As you can see, it's pretty straight forward, it checks for improperly formatted dates, dates with with days that don't exist in the month given, leap years, and other oddities like non-numeric characters. You can test the date and pull the formatted date in the most probable format (ie, dates like "01/03/04" could be translated into any number of valid dates, but it's probably January 3rd, 2004).
There may be some bugs, there's definitely room for improvement and optimization, and I'd like to add the ability to pull a timestamp for the given date (and negative timestamp for dates prior to the epoch). Anyway, without further ado, here's the GPL'ed PHP DateValidator v1.0:
Click here to download the PHP DateValidator v1.0 (2.7kb)