Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Date::create_from_string() timezone issue
  • For what it's worth:

    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 https://github.com/fuel/core/blob/1.7/develop/classes/date.php#L174 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();.
  • HarroHarro
    Accepted Answer
    Unix timestamps are always in UTC, other datetime's are always local time, this is how PHP works. Which means indeed you're borked if you have to work across timezones.

    The fact that you get -3600 back is already an indication that there's a bigger issue, since Europe/Berlin is currently GMT+2, so it should have returned -7200. Which is an indication strptime() is timezone aware, but doesn't do daylight savings.

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

    // create a date object using local (Europe/Berlin) timezone
    $date = \Date::createFromFormat('mysql', '1970-01-01 00:00:00');
    echo $date->getTimestamp(); // displays -3600
    echo $date->format('local'); // displays Thu Jan  1 00:00:00 1970
    echo $date->format('local', 'UTC')); // displays Wed Dec 31 23:00:00 1969

    // create a date object using UTC timezone, local is Europe/Berlin
    $date = \Date::createFromFormat('mysql', '1970-01-01 00:00:00', 'UTC');
    echo $date->getTimestamp(); // displays 0
    echo $date->format('local'); // displays Thu Jan  1 01:00:00 1970
    echo $date->format('local', 'UTC')); // displays Thu Jan  1 00:00:00 1970
  • That's great to know, Harro! It's awesome that someone from the team gets to check discussions in the forum. Looking forward to 2.0. Cheers!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion