The AttributeType::DateTime in model attributes saves as UTC which is great, but upon getting that attribute I receive a Craft\DateTime object with my server timezone set for it, and thus the time gets modified accordingly.

Is there any way I could force the model to return a Craft\DateTime object with the UTC timezone?

When I created a getter method public function getDate() it doesn't get called when using object.date inside a Twig template. I have to specifically call object.getDate() instead, which isn't really an option for me.

This is how my model defines attributes:

protected function defineAttributes()
    return array(
        'date'  => AttributeType::DateTime,

Maybe there are some additional rules I can set for it, that I am not aware of? I couldn't find anything in Yii's documentation.

1 Answer 1


I can't think of a way to automatically tell Craft to do this, but you could write a simple helper method to do this for you:

public static function convertDateTimeToUTC($dateTime)
    return $dateTime->setTimezone(new \DateTimeZone('UTC'));
  • Thanks a lot for the response, Brad, but unfortunately helper's a no-go. I tried overriding the magic getter for the Model and catch calls for that date, set its timezone to UTC and then continue on to the parent getter, but it doesn't work, even though the function gets called, and the timezone gets set to UTC. I'll think of another way, thanks!
    – Gustavs
    Commented Mar 21, 2016 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.