PHP -- The Date APIs Still Suck

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

PHP -- The Date APIs Still Suck

Post by volomike »

In PHP, it's my opinion that the date APIs still suck even in PHP 5.2. First, they've got bugs. Second, it's hard to do date diffs, age checks, age range checks, and date validation -- not without several functions and working out the kinks.

Take for instance this bug on my Ubuntu Linux with PHP 5.2.1:

Code: Select all

 
$s = '8-3-1968';
$s = date('Y-m-d', strtotime($s));
echo "$s<BR>\n";
 
That's supposed to show up as 1968-08-03, but it's showing up as 1968-03-08. See the month switched with the day? Perhaps I'm missing something, like perhaps I'm supposed to tell my date APIs to act American and not non-American on the dates? Don't know. But switch the first line from 8-3-1968 to 8/3/1968 and the bug goes away. Go figure.

And how many of us have stuck dates older than 1970 in and we start playing with it and end up with a date coming back with a year of 1969 or 1970 instead of something like 01/01/1888? Lots of us, that's who.

Luckily they've created the new DateTime() object, but you have to play with it to know its idiosyncrasies.
Post Reply