Pull DateTime field using alternate timezone? I have a question regarding populating models, in that I have a DateTime field in my model that I'm pulling / populating from the database. The DateTime value does not include the timezone it was set to when it was stored, its stored in UTC.

I have the original timezone value (Not the same as the CRAFT timezone but the users timezone that actually created the entry) stored in the model attributes as well.

Is there a way I can extend the model so that when the data is populated I can force the correct timezone on the DateTime fields? 

I'm pretty sure that Craft is going to convert them to the timezone craft is set to when it pulls it into the model, but I dont want that in this case. I want it set to the TZ stored in the "timeZone" column/attribute...

Example:

Craft Timezone is America/Chicago

The user's timezone is America/New_York

The user selects a date and time and the controller creates the DateTime object with the user's selection, specifying the timezone America/New_Yrok, then tells the model to save, so it stores it in the database.

The Database DateTime value is now stored in UTC.

When I fetch this data and populate a model with it and reference the DateTime object, its going to be in Amercia/Chicago right? since that's what the craft system is set to, right?

So what is the best way to set up the model so it automatically knows to use $this-&gt;getAttribute('timeZone') as the timezone for that field rather than the Craft system setting?

EDIT:

This is what I ended up doing that appears to work. I added this to my model:

public static function populateModel($values) {
    $model = PARENT::populateModel($values);

    /** @var $startTime DateTime */
    $startTime = $model-&gt;getAttribute('startTime');
    if($startTime) {
        $startTime-&gt;setTimezone($model-&gt;timeZone);
        $model-&gt;setAttribute('startTime', $startTime);
    }
    /** @var $endTime DateTime|null */
    $endTime = $model-&gt;getAttribute('endTime');
    if($endTime) {
        $endTime-&gt;setTimezone($model-&gt;timeZone);
        $model-&gt;setAttribute('endTime', $endTime);
    }

    return $model;
}
100% untested, but something like this should work:


Have your model override BaseModel's populateModel.
Find the DateTime field you're interested in and get its value.
Create a new DateTime object and pass in the value from the timeZone attribute.
Set the new DateTime object back on the original DateTime field.
Call parent::populateModel($values).