PHP date stuff
2009-08-04 12:38 am ∴ Programming ∴ by matt -

I’ve talked before about how I thought PHP’s date handling is the best thing about PHP.

Well, I kinda lied. It sucks…

It works fine if you’re dealing with dates in the here and now, but say you’re accepting user input for a date-of-birth field. strtotime() returns a signed integer, and if you look carefully, you’ll see that strtotime() will fail on early PHP5 revisions on Windows for dates before 1/1/1970.

So that’s problem 1. Problem 2 is going past 1/19/2038.  You would think that it wouldn’t be a problem on 64-bit systems, but I have no idea if that’s the case or not — I can’t seem to find any documentation on 64-bit versions of PHP using 64-bit timestamps.

Luckily, there’s a new DateTime class for PHP 5.2 and up. The constructor of the DateTime class accepts anything that strtotime() can, and DateTime::format() accepts a date() style format. So that’s easy enough. Hopefully, you deduce by my lack of griping, that Pre-epoch and post-2038 dates will work. I didn’t test to see how far back or forward, it can go. Far enough for my uses.

Why the new class isn’t listed on the date() and strtotime() pages — I have no idea. Hopefully now I won’t have another Epoch Fail.