RANDOM QUOTE
Ever notice that anyone going slower than you is an idiot, but anyone going faster is a maniac?

-George Carlin
Search
Sticky Posts
The Ghettotenna
SVG Icons
KNetworkLED
Brew Your Own Damn Beer
Latest Comments
linkapalooza (5 comments)
Objects in the Mirror (4 comments)
Doo Dah Doo Doo Doo Dah Dah Doo... Big News Coming Your Way!!!!!!!!!!!!!!!!!!!!!!!!!!! (3 comments)
SVG Icons (7 comments)
A Revolution in Taco Consumption (5 comments)
Links & Friends
Reddit
Kotaku
Hardgeus
PVP Online
Boing Boing
The Sneeze
Penny Arcade
Resources
glitch13.com :.::.: ..:.::. :.:::... Home | About | Feedback | Archive | RSS

PHP DATE VALIDATOR
Category: Tech
Thursday, March 3rd, 2005 @ 03:31 pm
Posted By Brent

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)


Comments

NAME: Brent
Tuesday, September 12th, 2006 @ 12:09 am
File Format? Are you having problems with it being a tar.bz2 file? It's a unix compression format, I believe Winrar for Windows can open them.

Let me know if it can't, I'll recompress it zip style and slap it up here.


NAME: Philip
WEBSITE: http://www.bozebo.com
Saturday, September 9th, 2006 @ 06:42 am
UUUHHH... stupid file format, i cant get it.


NAME: Philip
WEBSITE: http://www.bozebo.com
Saturday, September 9th, 2006 @ 06:41 am
Ecxellent! I am gonna check this out, I have been googling for a decent php date validator.


NAME: Anurag
WEBSITE: http://www.google.com
Friday, April 21st, 2006 @ 09:19 am
hmm, shall try this. hoping it works well :)


NAME:
EMAIL: *OPTIONAL*
WEBSITE: *OPTIONAL*
WEBSITE: *OPTIONAL*
Comment:
    
|