PHP Date Time
May 11, 2008 § Leave a comment
is more or less annoying. Before fanbois comment me to oblivion, let me explain why.
DateTime and DateTimeZone are objects. They can be created using new DateTime() and new DateTimeZone() calls.
Why is it not documented anywhere? Instead, the documentation points to global function calls.
Talk about intuitiveness. date() does not return DateTime object. It returns string, but of course, that’s how it always be.
Now let’s talk about timezone_open(…). If you pass an invalid parameter, it WILL return false, but, it will also throw:
Warning: timezone_open() [function.timezone_open]: Unknown or bad timezone
Damn, that behavior is reported here.
More about timezone, did you know that you can call setTimezone() on DateTime object? I found it out by lucky guess. Where’s that documented on php.net?
Finally, when calling dateObject->format(‘U’), I was expecting to get “Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)” –> pulled from php.net. But I didn’t get such results.
The seconds returned was not based on UTC timezone. It was based on the default php.ini, which was local timezone (PST).
So make sure before formatting to UTC, you call:
dateObject->setTimezone(new DateTimeZone(‘UTC’)) or
don’t even bother creating object, just use: gmmktime()
Have fun with DateTime!