<p>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:</p>

<pre><code>public static function convertDateTimeToUTC($dateTime)
{
    return $dateTime-&gt;setTimezone(new \DateTimeZone('UTC'));
}
</code></pre>

CC BY-SA 3.0
2016-03-21T07:07:57.167Z
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
CC BY-SA 3.0
2016-03-19T00:08:41.407Z
2016-03-19T00:08:41.407Z
Brad Bell
<p>The <code>AttributeType::DateTime</code> in model attributes saves as UTC which is great, but upon getting that attribute I receive a <code>Craft\DateTime</code> object with my server timezone set for it, and thus the time gets modified accordingly.  </p>

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

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

<p>This is how my model defines attributes: </p>

<pre><code>protected function defineAttributes()
{
    return array(
        'date'  =&gt; AttributeType::DateTime,
    )
}
</code></pre>

<p>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.</p>

<plugins><datetime><models><attribute>
Getting UTC DateTime from Model attributes
# Getting UTC DateTime from Model attributes

(Asked by: username_0 on 2016-03-18)

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.

**Tags:** plugins, datetime, models, attribute

---

## 1 answers

### Answer by username_1 on 2016-03-19. Score: 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'));
}

> **Comments:**
> **username_0**: 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!  

---

