<p>Here is some code used in the past. </p>

<pre><code>$timezone = new \DateTimeZone(craft()-&gt;getTimeZone());
$newdate = \DateTime::createFromFormat(DateTime::MYSQL_DATETIME, $mysqlDate, $timezone);
</code></pre>

<p>I have typically done this before assigning to a model. </p>

CC BY-SA 3.0
2017-01-14T05:37:37.657Z
2017-01-14T05:37:37.657Z
a-am
<p>I have a date stored in the database in UTC.</p>

<pre><code>2016-01-13 09:01:16
</code></pre>

<p>I can query the database and output this date just fine. But it outputs in the date in the wrong timezone.</p>

<p><strong>How do I adjust a date from the database into the current user's locale?</strong></p>

<p>I believe this involves using <code>craft()-&gt;timezone</code> at some point, with some method, but I have not had any luck. I'm retrieving the date with the query builder:</p>

<pre><code>$rows = craft()-&gt;db-&gt;createCommand()
         -&gt;select('dateCreated')
         -&gt;from('mytable')
         -&gt;queryAll();
</code></pre>

<p><strong>Bonus points</strong>: sometimes I have multiple dates, so I need to modify all dates I get back into the right timezone.</p>

<p><strong>More bonus points</strong>: at what point in the process is the best point to modify the date into the right timezone? In MySQL query? In PHP? In Twig?</p>

<localization><datetime>
How do I convert a date stored in UTC to the user's current locale in PHP?
# How do I convert a date stored in UTC to the user's current locale in PHP?

(Asked by: username_0 on 2017-01-14)

I have a date stored in the database in UTC.
2016-01-13 09:01:16

I can query the database and output this date just fine. But it outputs in the date in the wrong timezone.
How do I adjust a date from the database into the current user's locale?
I believe this involves using craft()->timezone at some point, with some method, but I have not had any luck. I'm retrieving the date with the query builder:
$rows = craft()->db->createCommand()
         ->select('dateCreated')
         ->from('mytable')
         ->queryAll();

Bonus points: sometimes I have multiple dates, so I need to modify all dates I get back into the right timezone.
More bonus points: at what point in the process is the best point to modify the date into the right timezone? In MySQL query? In PHP? In Twig?

**Tags:** localization, datetime


---

## 1 answers

### Answer by username_1 on 2017-01-14. Score: 1

Here is some code used in the past. 
$timezone = new \DateTimeZone(craft()->getTimeZone());
$newdate = \DateTime::createFromFormat(DateTime::MYSQL_DATETIME, $mysqlDate, $timezone);

I have typically done this before assigning to a model.


---

