🌐
MySQL
dev.mysql.com › doc › refman › 8.4 › en › datetime.html
MySQL :: MySQL 8.4 Reference Manual :: 13.2.2 The DATE, DATETIME, and TIMESTAMP Types
You can convert TIMESTAMP values to UTC DATETIME values when retrieving them using CAST() with the AT TIME ZONE operator, as shown here:
Discussions

mysql - Converting string to date with timezone adjustment - Database Administrators Stack Exchange
DELIMITER $$ DROP FUNCTION IF EXISTS ... function takes an input timestamp value with an offset formatted as above, -- and converts it to the equivalent MySQL datetime value, expressed in the current session's -- time zone. Since this is also the timezone that columns in the TIMESTAMP ... More on dba.stackexchange.com
🌐 dba.stackexchange.com
Convert MySQL datetime to timestamp - Stack Overflow
Set timezone to UTC and this runs without error, otherwise you may have datetimes that don't convert to a timestamp in the current time zone. More on stackoverflow.com
🌐 stackoverflow.com
How does everyone handle time zones with the database?
Storing them as unix or MySQL date time doesnt matter, just make sure its all UTC going in and all UTC coming out. Always UTC everywhere. Then in your PHP framework you just use whatever Date class they have available, or DateTime, or Carbon to take the America/Chicago value our of the users profile and set the timezone. If they are a facebook user and you dont want to ask them for a specific timezone then you can take the timezone offset from their user account and just update that value each time they log in, so it might be -4 for a while but next time they log in it could be -5. Remember this value should be A FLOAT because some awkward-ass islands are UTC+5.75 and have +30 minute summer offsets. If a user is logged out or you have no idea who they are then you could do some geo-lookup on their IP to find their timezone. But that is not fun, I just show UTC to those folks. More on reddit.com
🌐 r/PHP
56
46
June 24, 2012
How convert UTC Time to Local Time?
You can use CONVERT_TZ in mysql queries, however it requires first populating the timezone tables into MySQL (by reading them from the OS). You also need to rerun this periodically as some do change over time. See mysql's mysql_tzinfo_to_sql commandline tool. I don't like this method, although Id use it if I had to convert lots of data in batch If you just want to do it in PHP, easiest way is to use the DateTime class.. $tz_from = new DateTimeZone('UTC'); $tz_to = new DateTimeZone('Europe/London'); $orig_time = new DateTime($mysql_timestamp, $tz_from); $new_time = $orig_time->setTimezone($tz_to); $newtimestamp = $new_time->format('c'); More on reddit.com
🌐 r/PHP
8
1
June 12, 2014
🌐
TutorialsPoint
tutorialspoint.com › mysql › mysql_date_time_functions_convert_tz.htm
MySQL - CONVERT_TZ() Function
It is time standard and is commonly used across the world. MySQL provides a function called CONVERT_TZ() and it is used to convert the given date from one time zone to another time zone.
🌐
w3resource
w3resource.com › mysql › date-and-time-functions › mysql-convert_tz-function.php
MySQL CONVERT_TZ() function - w3resource
August 28, 2023 - The following statement will convert the datetime value 2008-05-15 12:00:00 from +00:00 timezone to +10:00 timezone. ... mysql> SELECT CONVERT_TZ('2008-05-15 12:00:00','+00:00','+10:00'); +-----------------------------------------------------+ | CONVERT_TZ('2008-05-15 12:00:00','+00:00','+10:00') | +-----------------------------------------------------+ | 2008-05-15 22:00:00 | +-----------------------------------------------------+ 1 row in set (0.02 sec)
🌐
MySQL
dev.mysql.com › doc › refman › 8.4 › en › date-and-time-functions.html
MySQL :: MySQL 8.4 Reference Manual :: 14.7 Date and Time Functions
If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC. The server interprets date as a value in the session time zone and converts it to an internal Unix timestamp value in UTC.
Top answer
1 of 2
1

How about a stored function?

DELIMITER $$

DROP FUNCTION IF EXISTS `ts_from_offset` $$
CREATE FUNCTION `ts_from_offset`(in_ts TINYTEXT) RETURNS datetime
NO SQL
DETERMINISTIC
BEGIN

-- Thu Oct 23 16:46:47 2014 +02:00

-- this function takes an input timestamp value with an offset formatted as above,
-- and converts it to the equivalent MySQL datetime value, expressed in the current session's
-- time zone.  Since this is also the timezone that columns in the TIMESTAMP data type expect,
-- this causes the input value to be stored correctly in the native TIMESTAMP format, which is.
-- UTC under the hood.

-- if you are taking the value here and stuffing it into a non-UTC DATETIME column, you need to have 
-- session @@time_zone set to the same zone in which that column should be stored, or use
-- CONVERT(ts_from_offset('input value'),'UTC','Your Desired Time Zone');

-- http://dba.stackexchange.com/questions/83898/converting-string-to-date-with-timezone-adjustment/84041#84041

DECLARE offset_string TINYTEXT DEFAULT NULL;
DECLARE date_string TINYTEXT DEFAULT NULL;
DECLARE offset_sign TINYINT DEFAULT NULL;
DECLARE offset_hours TINYINT DEFAULT NULL;
DECLARE offset_minutes TINYINT DEFAULT NULL;

SET offset_string = SUBSTRING_INDEX(in_ts,' ',-1);

SET in_ts = LEFT(in_ts, LENGTH(in_ts) - 1 - LENGTH(offset_string));

SET offset_sign = IF(SUBSTRING(offset_string FROM 1 FOR 1) = '+', -1, +1); # we need to flip the sign, to "back out" the offset to get a time in UTC
SET offset_hours = CAST(SUBSTRING(offset_string FROM 2 FOR 2) AS SIGNED) * offset_sign;
SET offset_minutes = CAST(SUBSTRING(offset_string FROM 5 FOR 2) AS SIGNED) * offset_sign;
RETURN CONVERT_TZ(DATE_ADD(DATE_ADD(STR_TO_DATE(in_ts,'%a %b %e %T %Y'), INTERVAL offset_hours HOUR), INTERVAL offset_minutes MINUTE),'UTC',@@time_zone);

END $$

DELIMITER ;

Example output...

mysql> SET @@TIME_ZONE = 'UTC';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT ts_from_offset('Thu Oct 23 16:46:47 2014 +02:00');
+---------------------------------------------------+
| ts_from_offset('Thu Oct 23 16:46:47 2014 +02:00') |
+---------------------------------------------------+
| 2014-10-23 14:46:47                               |
+---------------------------------------------------+
1 row in set (0.00 sec)

No warranty, but it seems like this should do the trick.

2 of 2
-1

Assuming v5.6 (and I don't think this has changed), this is probably what you need to know (split up a bit, MySQL's docs tend to write HUGE blocks of text):

http://dev.mysql.com/doc/refman/5.6/en/datetime.html

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)

By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis.

As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions.

The current time zone is available as the value of the time_zone system variable.

So, if you store a value (1023) when your session is set to UTC, and retrieve it from a session set to EST, you should get back the same value with -0500 (0523)

🌐
MySQL
dev.mysql.com › doc › refman › 8.0 › en › datetime.html
MySQL :: MySQL 8.0 Reference Manual :: 13.2.2 The DATE, DATETIME, and TIMESTAMP Types
May 6, 2022 - In MySQL 8.0.22 and later, you can convert TIMESTAMP values to UTC DATETIME values when retrieving them using CAST() with the AT TIME ZONE operator, as shown here:
Find elsewhere
🌐
Medium
medium.com › @kenny_7143 › time-zone-in-mysql-e7b73c70fd4e
Time Zone in MySQL. DATETIME and TIMESTAMP | by Kenny Shi | Medium
March 18, 2020 - For example, mysql> SET time_zone='UTC'; Query OK, 0 rows affected (0.00 sec)mysql> SELECT timezone, CONVERT_TZ(time_datetime, timezone, @@SESSION.time_zone) as time_datetime, CONVERT_TZ(time_timestamp, @@SESSION.time_zone, 'America/New_York') ...
🌐
A2 Hosting
a2hosting.com › kb › developer-corner › mysql › convert-the-mysql-time-zone
How to convert the time zone in MySQL
On shared servers, you cannot change the default MySQL time zone, because this would affect other accounts on the server. However, you can convert the time zone used in DATE, TIME, and DATETIME fields by calling the CONVERT_TZ function as needed.
🌐
Epoch Converter
epochconverter.com › epoch converter › programming a-z › using unix timestamps in mysql mini-course
Using Unix Timestamps in MySQL Mini-Course - Epoch Converter
When converting normal dates to epoch use TIMESTAMPDIFF: SELECT TIMESTAMPDIFF(second,FROM_UNIXTIME(0),'1960-01-01 00:00:00' ); Replace the 1960 date with your date in your local timezone (MySQL time_zone).
🌐
MySQL
dev.mysql.com › doc › refman › 9.6 › en › datetime.html
MySQL :: MySQL 9.6 Reference Manual :: 13.2.2 The DATE, DATETIME, and TIMESTAMP Types
You can convert TIMESTAMP values to UTC DATETIME values when retrieving them using CAST() with the AT TIME ZONE operator, as shown here:
🌐
MySQL
dev.mysql.com › doc › refman › 8.0 › en › time-zone-support.html
MySQL :: MySQL 8.0 Reference Manual :: 7.1.15 MySQL Server Time Zone Support
April 26, 2022 - Nor are values in those data types stored in UTC; the time zone applies for them only when converting from TIMESTAMP values. If you want locale-specific arithmetic for DATE, TIME, or DATETIME values, convert them to UTC, perform the arithmetic, and then convert back.
🌐
Ubiq BI
ubiq.co › home › how to convert utc to local time in mysql
How to Convert UTC to Local Time in MySQL - Ubiq BI
September 1, 2025 - Here’s an example to convert EST to Paris timezone by specifying time zone names instead of offset values. mysql> select convert_tz('2020-09-17 03:00:00','US/Eastern','Europe/Paris'); You can also use system functions like now() in convert_tz ...
🌐
PlanetScale
planetscale.com › blog › datetimes-vs-timestamps-in-mysql
Datetimes versus timestamps in MySQL — PlanetScale
June 22, 2023 - MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
🌐
MySQL
dev.mysql.com › doc › refman › 9.1 › en › datetime.html
MySQL :: MySQL 9.1 Reference Manual :: 13.2.2 The DATE, DATETIME, and TIMESTAMP Types
You can convert TIMESTAMP values to UTC DATETIME values when retrieving them using CAST() with the AT TIME ZONE operator, as shown here:
🌐
MySQL
dev.mysql.com › doc › refman › 8.0 › en › date-and-time-functions.html
MySQL :: MySQL 8.0 Reference Manual :: 14.7 Date and Time Functions
January 18, 2022 - If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC. The server interprets date as a value in the session time zone and converts it to an internal Unix timestamp value in UTC.
🌐
PopSQL
popsql.com › learn-sql › mysql › how-to-convert-utc-to-local-time-zone-in-mysql
How to Convert UTC to Local Time Zone in MySQL - PopSQL
Learn how to convert timestamps from UTC to local time zones in MySQL using the CONVERT_TZ function. Manage time zone differences effortlessly in your database queries.
🌐
Medium
danuka-praneeth.medium.com › guide-to-time-zones-conversion-between-zones-and-storing-in-mysql-da4fc4350cd9
Guide to Time-zones, Conversion between Zones and Storing in MySQL | by Danuka Praneeth | Medium
September 22, 2019 - In MySQL5+, TIMESTAMP values are converted from the session time zone to UTC for storage, and from UTC to the session time zone for retrieval. But DATETIME does not do any conversion. As a result TIMESTAMP differs with current timezone settings ...
🌐
Five
five.co › home › blog › sql timestamp to date conversion
SQL Timestamp to Date Conversion: Practical Guide - Five
October 2, 2024 - In MySQL and MariaDB, you can use the DATE() function as shown above, or try these alternatives: SELECT CAST(timestamp_column AS DATE) AS converted_date, DATE_FORMAT(timestamp_column, '%Y-%m-%d') AS formatted_date FROM your_table;