Last Updated: February 25, 2016
·
682
· zejesago

Date::create_from_string() Timezone Issue

I'm using FuelPHP 1.6.1 on my Mac. When I use \Date::create_from_string('1970-01-01 00:00:00', 'mysql')->get_timestamp(); it converts the datetime to the locale timezone before sending it to \Date::forge();. Hence, it returns -3600 instead of 0; \Fuel::$timezone is set to "Europe/Berlin".

The function is relatively useless since it works on the assumption that the string's timezone is the same as the timezone set by \Fuel::init();. There are times that this is not the case, e.g. when MySQL server is on UTC but the PHP server is on a different timezone. It'd be better if there was an extra timezone parameter (at least defaulting to UTC) to set the timezone temporarily before mktime() at Line 174 of core/classes/date.php is called, especially when you expect timestamps to ALWAYS be in GMT, which \Date::forge() does.

As a workaround, I just call \Date::forge(strtotime('01 Jan 1970 00:00:00 UTC'), 'UTC'); to make it more usable by other functions like format();.

(Originally posted at FuelPHP Forums.)


Harro Verton, the project lead for FuelPHP, then replied,

This is already addressed in the 2.0 codebase, where Date has been completely rewritten to be (amongst other things) be fully timezone aware.

FuelPHP just released 1.7 on October, 2013, and we could likely see 2.0 out by next year. For now, just make sure you've got your timezone cases properly tested before going into production. Cheers!