PHP DateTime is missing methods in 5.2

It turns out that in PHP 5.2, the DateTime class is missing a couple of methods (get/setTimestamp) that are available in PHP 5.3. These are some strange methods to be missing as a lot of people in the PHP world seem to work on Timestamps using these concepts, so you would have thought that php would have included these methods initially. Unfortunately they did not, so here is a fix for that:

And since using it in production would be no good if it were not tested properly, here are the unit tests to make sure that it returns the same values as the new one in PHP 5.3:

You use it the same way that you would use the normal DateTime class, it even includes the poorly named php functions date_timestamp_get / date_timestamp_set if they don’t exist so you can use them as well.

Hopefully this is helpful for those of you that have used this functionality mistakenly (as I did) before realizing that it wasn’t actually available.

(get it on github: http://gist.github.com/349273)


flattr this!

2 thoughts on “PHP DateTime is missing methods in 5.2”

Leave a Reply