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.

The global function for new DateTime is date_create(), while global function for new DateTimeZone is timezone_open(). How intuitive.

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

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 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

dateObject->setTimezone(timezone_open(‘UTC’)) or

don’t even bother creating object, just use: gmmktime()

Have fun with DateTime!



Tagged: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

What’s this?

You are currently reading PHP Date Time at RAPD.


%d bloggers like this: