Yup, there's the convert_tz function.
Yup, there's the convert_tz function.
For those unable to configure the mysql environment (e.g. due to lack of SUPER access) to use human-friendly timezone names like "America/Denver" or "GMT" you can also use the function with numeric offsets like this:
CONVERT_TZ(date,'+00:00','-07:00')
mysql - Converting string to date with timezone adjustment - Database Administrators Stack Exchange
Convert MySQL datetime to timestamp - Stack Overflow
How does everyone handle time zones with the database?
How convert UTC Time to Local Time?
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.
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)
UPDATE table1 A, table2 B SET B.date_added=UNIX_TIMESTAMP(A.date_added) WHERE A.id=B.id;
UNIX_TIMESTAMP('2015-01-15 12:00:00');
is sufficient to convert a mysql datetime to a Timestamp.
Try this please:
UPDATE table1 A, table2 B
SET B.date_added = FROM_UNIXTIME(A.date_added)
WHERE A.id=B.id
Reference. It seems like you have an issue with the way you format date stammp. Also please look into this post: Should I use field 'datetime' or 'timestamp'?