SQL FROM_UNIXTIME Not Correct - mysql

I have the following SQL:
FROM_UNIXTIME(1392354000)
When I insert that into the database in my insert statement, the value that gets saved is:
2014-02-13 19:00:00
However, this is not correct, because it should be like this:
2014-02-14 00:00:00
If you copy and paste the timestamp at http://www.unixtimestamp.com/index.php it will show the 14th of Feb.
What can I do to make my timestamp be the 14th of Feb (not the 13th)? Preferably, it would be as shown above (2014-02-14 00:00:00).

This is happening because the timezone of your mysql is not same as the URL you are testing with.
In the URL the timezone is UTC.
So to test this you can try this
First check the timezone set for your mysql server with the following command
SELECT ##global.time_zone;
This will show you the timezone result, and if its set to UTC you will get something as
+--------------------+
| ##global.time_zone |
+--------------------+
| +00:00 |
+--------------------+
else you will get some other result.
Now if its something else which is more likely as you mentioned
Try this
SET ##global.time_zone='+00:00';
Now your mysql server is having UTC timezone and then run the command
mysql> select FROM_UNIXTIME(1392354000) ;
+---------------------------+
| FROM_UNIXTIME(1392354000) |
+---------------------------+
| 2014-02-14 10:30:00 |
+---------------------------+
Here are few ways to change the timezone of your mysql server
http://www.inmotionhosting.com/support/website/databases/how-to-change-mysql-server-time-zone

Related

MySQL Update Error Incorrect datetime Value

Our database uses '0000-00-00 00:00:00' as the default value for many datetime and timestamp fields. MySQL has apparently decided that they only want us to use a valid date or null for these types of fields.
However, the '0000-00-00 00:00:00' values used to be acceptable and our code checks for this value. When I setup a new server, I edit the /etc/mysql/mysql.conf.d/mysqld.cnf file and add one line to the [mysqld] section.
sql_mode=NO_ENGINE_SUBSTITUTION
Today I have attempted to setup a new server. I added the sql_mode=NO_ENGINE_SUBSTITUTION to the MySQL configuration and restarted the mysql service. However, this new server only gets errors.
UPDATE example_table SET active = 1 WHERE example_table_id = 1;
ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' for column 'my_date_field' at row 1
I could of course update the database to have NULL values or a "valid" default date such as '1970-01-01 00:00:01', but this would break existing code that checks the data for '0000-00-00 00:00:00'.
Additional Example Information:
Server type: MySQL
Server version: 5.7.22-0ubuntu0.16.04.1 - (Ubuntu)
Protocol version: 10
innodb_version: 5.7.22
SELECT my_date_field FROM example_table WHERE active = 1;
+---------------------+
| my_date_field |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
DESC example_table;
+------------------------------------+--------------+------+-----+---------------------+
| Field | Type | Null | Key | Default |
+------------------------------------+--------------+------+-----+---------------------+
| my_date_field | datetime | NO | | 0000-00-00 00:00:00 |
| active | tinyint(2) | NO | | 1 |
+------------------------------------+--------------+------+-----+---------------------+
I've got other machines on the same version of MySQL working with just sql_mode=NO_ENGINE_SUBSTITUTION and I'm almost to the point of just imaging one of those for this new machine, until such time as the code is updated to look for a valid date or null instead of '0000-00-00 00:00:00'.
The NO_ENGINE_SUBSTITUTION SQL mode doesn't have anything to do with dates. It only did the trick for you because by setting it you also removed the server's default mode, which in modern versions is something on the line of TRADITIONAL, which is a combination mode that, among others, includes NO_ZERO_IN_DATE and NO_ZERO_DATE.
You possibly just want to set a legacy mode for this application at session level, e.g.:
SET ##SESSION.sql_mode = '';

Unable to set the UTC timezone permamently

I am using this time-zone-support for setting the UTC timezone on my machine,
But the trouble is that I am not able to set it permanently, it changes to System time when I restart the MySQL server.
The box is OpenSuse 12.3, and the MySQL version is 5.5.33
mysql> SET time_zone = UTC;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT ##global.time_zone, ##session.time_zone;
+--------------------+---------------------+
| ##global.time_zone | ##session.time_zone |
+--------------------+---------------------+
| UTC | UTC |
+--------------------+---------------------+
1 row in set (0.00 sec)
now when I restart the mysql server, it reverts back to system.
After restarting the server:
mysql> SELECT ##global.time_zone, ##session.time_zone;
+--------------------+---------------------+
| ##global.time_zone | ##session.time_zone |
+--------------------+---------------------+
| SYSTEM | SYSTEM |
+--------------------+---------------------+
1 row in set (0.00 sec)
I have tried doing the default time zone as well, but its giving me error.
mysql> default-time-zone=UTC;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'default-time-zone=UTC' at line 1
mysql>
Can someone please let me know what am I missing here, and how to set the UTC timezone permanently.
I am answering my own question, as in the pursuit of the answer I have found the solution here
I was editing my.cnf file and entering default-time-zone = UTC at the end of the file, as I did in windows machine, it does NOT work in Linux/Opensuse.
Then I entered default-time-zone = UTC in the [mysqld] section of the my.cnf, and the new time zone is picked up, and now the UTC timezone is set permanently.

MySQL: Get local time for a specific time zone

Here's a simple version of the table users:
+--------------+-------------------+
| id | timezone |
+--------------+-------------------+
| 1 | 'Europe/Helsinki' |
| 2 | 'Europe/Paris' |
+--------------+-------------------+
I want to know what's the local time for each one of these users (depending on their time zones), so that I can select users for who it's 4pm for example.
I'm using the LAMP stack, but I'd like to do that using MySQL only (not selecting all users and running them in a PHP loop).
Use the CONVERT_TZ for this:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz
SELECT * FROM users WHERE hour(CONVERT_TZ(now(), server_tz, `timezone`))=16
You can change the timezone with set time_zone:
mysql> set time_zone='Europe/Helsinki';
mysql> select now();
2012-09-21 16:15:06
mysql> set time_zone='Europe/Paris';
mysql> select now();
2012-09-21 15:15:40
Using this you can, for example, define a function that returns the current time for the user's timezone:
create function current_time_in_tz(tz varchar(40)) returns datetime
begin
set #old_tz = ##session.time_zone;
set time_zone=tz;
set #now = now();
set time_zone=#old_tz;
return #now;
end
select id, current_time_in_tz(timezone) from users;
Note that DATE, TIME and DATETIME values don't depend on the time zone, so values from columns of these types are not automatically adjusted when querying. TIMESTAMP values are adjusted:
mysql> create temporary table tbl (dt datetime, ts timestamp);
mysql> insert into tbl values (now(),now());
mysql> select * from tbl;
+---------------------+---------------------+
| dt | ts |
+---------------------+---------------------+
| 2012-09-21 15:21:56 | 2012-09-21 15:21:56 |
+---------------------+---------------------+
mysql> set time_zone='Europe/Helsinki';
mysql> select * from tbl;
+---------------------+---------------------+
| dt | ts |
+---------------------+---------------------+
| 2012-09-21 15:21:56 | 2012-09-21 16:21:56 |
+---------------------+---------------------+
If set time_zone fails with this error:
ERROR 1298 (HY000): Unknown or incorrect time zone: 'Europe/Helsinki'
you need to load the time zone info into mysql with a command like this:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
For more info see http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
A more generic (not depending on the timezone of the server) solution than Nin's answer would be:
SELECT * FROM users WHERE hour( CONVERT_TZ(UTC_TIMESTAMP(), 'UTC', timezone) )=16
It would be nice if MySQL had a function like NOW_TZ(timezone).

mysql NOW() return wrong value whereas global timezone is set +00:00

since 2 weeks I puzzled over timezone issue, everything working fine on my localhost BUT it returns wrong value on dedicated server. Let me tell what i have done so far.
First set global timezone by below query: ( Super privilege both on localhost and server )
SET GLOBAL time_zone = '+00:00';
now run below query to cross check whatever done
SELECT NOW(),##global.time_zone AS gtz,##session.time_zone AS stz,
TIMEDIFF(NOW(), CONVERT_TZ( NOW() , ##session.time_zone , '+00:00' ) )
AS OFFSET;
but it display different results on local and dedicated server
on localhost (192.168.x.x) mysql version : 5.5.8
+---------------------+--------+--------+----------+
| NOW() | gtz | stz | OFFSET |
+---------------------+--------+--------+----------+
| 2012-07-02 07:06:55 | +00:00 | +00:00 | 00:00:00 |
+---------------------+--------+--------+----------+
1 row in set (0.00 sec)
on dedicated server (182.168.x.x) mysql version :5.1.53-log
+---------------------+--------+--------+----------+
| NOW() | gtz | stz | OFFSET |
+---------------------+--------+--------+----------+
| 2012-07-02 12:37:59 | +00:00 | +00:00 | 00:00:00 |
+---------------------+--------+--------+----------+
My question is
why NOW() gives wrong time ( above is IST ) whereas
timezone is set to +00:00 ?
side note :
I run below query
SHOW VARIABLES LIKE '%time%zone%';
on localhost
+------------------+---------------------+
| Variable_name | Value |
+------------------+---------------------+
| system_time_zone | India Standard Time |
| time_zone | +00:00 |
+------------------+---------------------+
on server
+------------------+---------------------+
| Variable_name | Value |
+------------------+---------------------+
| system_time_zone | GMT+5 |
| time_zone | +00:00 |
+------------------+---------------------+
does this will affect the result? OR
is there any bug in earlier version of mysql ?
please help me.
When calling NOW() (and related functions), MySQL converts the computer's system clock to the session timezone:
If the system clock is set to 12:30+05:30 and the session timezone is +00:00, the result will be 07:00.
If the system clock is set to 17:30+05:00 and the session timezone is +00:00, the result will be 12:30.
However, one can 'fool' MySQL into thinking that the system clock is in a different timezone to that which the operating system believes by using the --timezone command line argument to mysqld_safe:
If the system clock is set to 17:30+10:30 and the session timezone is +00:00, but MySQL was started in such a way specifying that the system clock should be understood to be GMT+5, the result will be the same as the second bullet above.
You should therefore check:
That the timezone of the system clock reconciles with the value given in the system_time_zone system variable (if it doesn't, ensure that you are not specifying a --timezone argument to mysqld_safe and that there was no TZ environment variable set when mysqld_safe was invoked);
That the system clock is reporting the correct time in its specified timezone.
You just need to restart mysqld after altering timezone of System..
The Global time zone of MySQL takes timezone of System. When you change any such attribute of system, you just need a restart of Mysqld.
That's it.
1) Change your system time and timezone, if necessary.
2) Open mysql console and put in your timezone, smth like this: SET GLOBAL time_zone = "+04:00";
3) Restart mysql.
i have tested-
SET time_zone='+05:30'; it is working,but i dont have global permission so you try by setting server time +05:30 ->
SET GLOBAL time_zone = '+05:30';

Can MySQL convert a stored UTC time to local timezone?

Can MySQL convert a stored UTC time to local time-zoned time directly in a normal select statement?
Let's say you have some data with a timestamp (UTC).
CREATE TABLE `SomeDateTable` (
`id` int(11) NOT NULL auto_increment,
`value` float NOT NULL default '0',
`date` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)
Then when I
"select value, date from SomeDateTable";
I of course get all the dates as in their stored UTC form.
But let's say that I would like to have them in another timezone (with DST),
can I then add some magic to the select query so that I get all the dates back in the selected timezone?
"select value, TIMEZONE(date, "Europe/Berlin") from SomeDateTable";
Or must I do this in some other layer on top, like in some php code?
(it seems to be how most people have solved this problem).
If your MySQL installation allows you to use CONVERT_TZ it is a very clean solution,
this example shows how to use it.
SELECT CONVERT_TZ( '2010-01-01 12:00', 'UTC', 'Europe/Stockholm' )
However I don't know if this is a good way since some MySQL installation is missing this function, use with care.
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')
One can easily use
CONVERT_TZ(your_timestamp_column_name, 'UTC', 'your_desired_timezone_name')
For example:
CONVERT_TZ(timeperiod, 'UTC', 'Asia/Karachi')
Plus this can also be used in WHERE statement and to compare timestamp i would use the following in Where clause:
WHERE CONVERT_TZ(timeperiod, 'UTC', '{$this->timezone}') NOT BETWEEN {$timeperiods['today_start']} AND {$timeperiods['today_end']}
select convert_tz(now(),##session.time_zone,'+03:00')
For get the time only use:
time(convert_tz(now(),##session.time_zone,'+03:00'))
1. Correctly setup your server:
On server, su to root and do this:
# mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql
(Note that the command at the end is of course mysql , and, you're sending it to a table which happens to have the same name: mysql.)
Next, you can now # ls /usr/share/zoneinfo .
Use that command to see all the time zone info on ubuntu or almost any unixish server.
(BTW that's the convenient way to find the exact official name of some time zone.)
2. It's then trivial in mysql:
For example
mysql> select ts, CONVERT_TZ(ts, 'UTC', 'Pacific/Tahiti') from example_table ;
+---------------------+-----------------------------------------+
| ts | CONVERT_TZ(ts, 'UTC', 'Pacific/Tahiti') |
+---------------------+-----------------------------------------+
| 2020-10-20 16:59:57 | 2020-10-20 06:59:57 |
| 2020-10-20 17:02:59 | 2020-10-20 07:02:59 |
| 2020-10-20 17:30:08 | 2020-10-20 07:30:08 |
| 2020-10-20 18:36:29 | 2020-10-20 08:36:29 |
| 2020-10-20 18:37:20 | 2020-10-20 08:37:20 |
| 2020-10-20 18:37:20 | 2020-10-20 08:37:20 |
| 2020-10-20 19:00:18 | 2020-10-20 09:00:18 |
+---------------------+-----------------------------------------+
I propose to use
SET time_zone = 'proper timezone';
being done once right after connect to database. and after this all timestamps will be converted automatically when selecting them.