I have a database containing times (ex: 2013-07-10 23:25:36)
They're all in Mountain Standard Time (Calgary) and I need to convert them to UTC.
I've tried to use the following statement to do so, and it resets them all to
0000-00-00 00:00:00
UPDATE assets_time SET time=convert_tz(time, 'MST', 'UTC')
I would appreciate any advice, thanks
According to this article:
The value can be given as a named time zone, such as 'Europe/Helsinki', 'US/Eastern', or 'MET'. Named time zones can be used only if the time zone information tables in the mysql database have been created and populated.
So this might be your problem. Also have you tried imputing numbers instead? Like this for example:
mysql>UPDATE assets_time SET time=CONVERT_TZ(time,'-07:00','+00:00');
You must use the standardize format:
UPDATE assets_time SET time=convert_tz(time, 'US/Mountain', 'UTC')
SELECT DATE_ADD(NOW(), INTERVAL -7 HOUR);
General syntax
SELECT DATE_ADD(NOW(), INTERVAL HOUR);
http://www.worldtimebuddy.com/utc-to-pst-converter
since you need for MST it is "-7"
Related
I want to update a date field and set it to 2018-03-22 00:00:00 but I get the following stupid error:
Error Code: 1292. Incorrect datetime value: '2018-03-22 00:00:00' for column 'Date' at row 158917
This is the query I use for updating:
update assets.transactions
set date = date_add(date, interval 1 hour)
where date between '2018-03-21 23:00:00' and '2018-06-29 23:59:59';
What is wrong? I searched a lot and found out dates before 1970-01-01 00:00:01 are not supported by MySQL, that is acceptable, but a date in the middle of 2018? That's something I can't digest.
Is there any solution to make this work right?
I guess you're updating a TIMESTAMP column. I also guess you have your MySQL instance set to a timezone with a daylight time switchover on 23-March-2018. I guess the rules of your timezone switchover in your country mean that the clock rolls over from 21-March-2018 11:59:59 to 22-March-2018 01:00:00.
So the value 2018-03-22 00:00:00 just doesn't exist.
Strange, isn't it?
Try issuing this MySQL command, to set the time zone to UTC, before doing these sorts of mass timestamp updates.
SET time_zone = 'UTC';
Don't forget to switch it back before doing other operations. Or just do those operations from a different MySQL connection.
I'm trying to convert a datetime from Asia/Manila to EST timezone
without declaring the exact interval like
date_sub(), subdate(), date_add(), adddate()
i find it easy to use
SELECT DATE_SUB('2016-04-04 13:00:00', INTERVAL 12 HOUR);
the result will be2016-04-04 01:00:00
But Im trying to create a dynamic script where i don't need to look how many hours is the difference between two timezone
and i find Convert_TZ() to do job
SELECT CONVERT_TZ('2016-04-04 13:00:00', 'Asia/Manila', 'EST');
but the result of this query is 2016-04-04 00:00:00
Maybe this native function is not including the "Daylight saving time(DST)"
Does anyone know how to do the trick?
where i can easily convert the time including the DST
to any timezone without hard coding the interval hour between the two timezone?
Thanks
Okay, my problem is solved, i use two option
First :
I simply use 'US/Eastern' not 'EST' to include the daylight in conversion.
Second:
Because I didn't know the first option earlier i do this to solve my problem at first.
I create a table that compose of the date where it is DST
which i found in some site online..
Then
I create a mysql function where its lookup to the table above
which if the specified date is between that DST Start and DST End it will automatically add 1 hour,
My function is like this,
CREATE FUNCTION usp_Convert(specified_date DATETIME, From_Timezone VARCHAR(20), To_Timezone VARCHAR(20), is_DST INT(1)) RETURNS datetime
DECLARE theDate DATETIME;
SET theDate = CONVERT_TZ(specified_date, From_Timezone, To_Timezone);
IF is_DST = 1 AND To_Timezone= 'EST' THEN
SET theDate = ADDDATE(theDate, INTERVAL 1 HOUR); END IF;
RETURN theDate;
This might not be the best answer but this totally solved my problem
Thanks.
first of all, I know that my question is very similar to that one:
MySQL select rows from exactly 7 days ago
the difference is that my dates are stored in the database as a timestamp.
I know that I can use FROM_UNIXTIME to get the date from the timestamp, the thing is, in another answer I read that was very resource consuming (because the timestamp field has to be converted to date in all the records before comparing).
DATE(from_unixtime(timestamp)) = CURRENT_DATE()
Is there any optimized way to do this?
Turn it around: calculate the unix timestamp of the target date first and use that.
WHERE timestamp = UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)
MySQL should calculate that value once and use it all the time (needs testing though). If it doesn't, use a variable or code.
Is there a MySQL function which can be used to convert a Unix timestamp into a human readable date? I have one field where I save Unix times and now I want to add another field for human readable dates.
Use FROM_UNIXTIME():
SELECT
FROM_UNIXTIME(timestamp)
FROM
your_table;
See also: MySQL documentation on FROM_UNIXTIME().
What's missing from the other answers (as of this writing) and not directly obvious is that from_unixtime can take a second parameter to specify the format like so:
SELECT
from_unixtime(timestamp, '%Y %D %M %H:%i:%s')
FROM
your_table
I think what you're looking for is FROM_UNIXTIME()
Need a unix timestamp in a specific timezone?
Here's a one liner if you have quick access to the mysql cli:
mysql> select convert_tz(from_unixtime(1467095851), 'UTC', 'MST') as 'local time';
+---------------------+
| local time |
+---------------------+
| 2016-06-27 23:37:31 |
+---------------------+
Replace 'MST' with your desired timezone. I live in Arizona 🌵 thus the conversion from UTC to MST.
Why bother saving the field as readable? Just us AS
SELECT theTimeStamp, FROM_UNIXTIME(theTimeStamp) AS readableDate
FROM theTable
WHERE theTable.theField = theValue;
EDIT: Sorry, we store everything in milliseconds not seconds. Fixed it.
You can use the DATE_FORMAT function. Here's a page with examples, and the patterns you can use to select different date components.
Easy and simple way:
select from_unixtime(column_name, '%Y-%m-%d') from table_name
Since I found this question not being aware, that mysql always stores time in timestamp fields in UTC but will display (e.g. phpmyadmin) in local time zone I would like to add my findings.
I have an automatically updated last_modified field, defined as:
`last_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Looking at it with phpmyadmin, it looks like it is in local time, internally it is UTC
SET time_zone = '+04:00'; // or '+00:00' to display dates in UTC or 'UTC' if time zones are installed.
SELECT last_modified, UNIX_TIMESTAMP(last_modified), from_unixtime(UNIX_TIMESTAMP(last_modified), '%Y-%c-%d %H:%i:%s'), CONVERT_TZ(last_modified,##session.time_zone,'+00:00') as UTC FROM `table_name`
In any constellation, UNIX_TIMESTAMP and 'as UTC' are always displayed in UTC time.
Run this twice, first without setting the time_zone.
If you would like to convert time AND display the data in a specific format you can use this string.
date_format(convert_tz(from_unixtime(TIMESTAMP), 'UTC', 'DESIRED TZ'), '%m/%d/%y')
where you add convert_tz to a date_format string. the %m/%d/%y being month/day/year.
you can find all the specific formats here https://www.w3schools.com/sql/func_mysql_date_format.asp
I am using the NOW() with insert query so that I can get the currentTime when the query is triggered. But I get a time of 12 hours back in the database. How can I get the current Time ? Please help.
Perform this query right after you've been connected to the database:
SET time_zone = '+05:30';
Where it is your valid timezone offset in the quotes.
This might happen if the Timezone in your server configuration is different. Assuming that you are on a shared host, try adding the following lines when you are querying:
mysql_query("SET time_zone = '+05:30';");
Source: http://wiki.dreamhost.com/Running_web_scripts_in_your_Timezone
Else try adding the following to your .htaccess file
SetEnv TZ Asia/Colombo
A full list of all the Timezones http://www.php.net/manual/en/timezones.php
You can do something like this
SELECT * FROM tbl WHERE datetime < NOW() - INTERVAL 12 HOURS
I believe you can do, in your SQL query:
DATE_ADD(now(), INTERVAL 12 HOUR);
This will take the current time and subtract add 12 hours from to it.