Asa Carter
Asa Carter

Reputation: 2225

Display MySQL datetime in users timezone

MySQL datetimes created using now() are stored as UTC.

Client side timezone is detected using JavaScript and returned as "Europe/London".

What is the best way to select the datetime from MySQL and display the created time in the users local time?

PHP and Node are both used with MySQL.

Upvotes: 0

Views: 304

Answers (2)

machineaddict
machineaddict

Reputation: 3236

I don't know how you logic is made, but I'm using this in PHP:

function utc_to_timezone($format, $date, $to_timezone)
{
    $date = new DateTime($date);
    $date->setTimezone(new DateTimeZone($to_timezone));

    return $date->format($format);
}

Example:

echo utc_to_timezone('Y-m-d H:i:s', '2014-07-04 15:34:23', 'Europe/London');

Upvotes: 1

Giulio De Marco
Giulio De Marco

Reputation: 137

You can use MySQL CONVERT_TZ function

SELECT CONVERT_TZ(NOW(), 'UTC', 'Europe/London')

Upvotes: 3

Related Questions