lox / pheasant

A lightweight data mapper designed to take advantage of PHP 5.3+
http://getpheasant.com
MIT License
101 stars 21 forks source link

Unmarshalling DateTime's when using different timezones #152

Closed bjornpost closed 8 years ago

bjornpost commented 9 years ago

Our configuration:

When unmarshalling datetimes, a DateTime object is created with just the datetime string returned by MySQL (i.e. 2015-06-30 15:38:29, which is +0000). As no TZ info is provided, PHP assumes the provided string is in the default_timezone, so 2015-06-30 15:38:29 becomes 2015-06-30 15:38:29+02:00. Whoops :-).

Right now, when connecting, I'm setting the correct timezone:

$p = \Pheasant::setup('mysql://user:pass@host/table');
$p->connection()->execute("SET time_zone='".date_default_timezone_get()."';");

Thoughts?

bjornpost commented 9 years ago

Thoughts, @jud @lox?

Jud commented 9 years ago

@bjornpost I think this would be something to document, but not do automatically.

bjornpost commented 9 years ago

@Jud why not do this automatically? I think it reduces the chance of unwanted/unpredictable side effects?

Jud commented 9 years ago

Might be best to follow suit and do something like ActiveRecord's config.active_record.default_timezone, which may either be set to :utc or :local.

Could allow the option to be set when doing Pheasant::setup.

lox commented 9 years ago

That reminds me why I've always avoided mysql timestamps :)

I think @Jud's suggestion is the right approach, I'd have a timezone somewhere for the database connection (set it here https://github.com/lox/pheasant/blob/master/lib/Pheasant/Database/Mysqli/Connection.php#L125) and then use that to correctly unmarshal timestamp's into the right tz.

bjornpost commented 8 years ago

Closing this for now.