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()

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?

1 Answer 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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