How to make MySQL's NOW() and CURDATE() functions use UTC?


To make MySQL’s NOW() and CURDATE() functions use UTC, you need to write my.cnf file. Write the below instruction in my.cnf −

[mysqld_safe]
timezone = UTC

Firstly, reach the directory with the help of the following query −

mysql> select @@datadir;

The following is the output −

+---------------------------------------------+
| @@datadir                                   |
+---------------------------------------------+
| C:\ProgramData\MySQL\MySQL Server 8.0\Data\ |
+---------------------------------------------+
1 row in set (0.00 sec)

Now reach the directory for which the link we got as the result of a query. The screenshot of my.cnf file on my Windows system −

Place option( [mysqld_safe] timezone = UTC ) under “mysqld”. You do not have to write on every page load. After placing it once in “my.cnf” file, you do not need to mention it again and again. It will work for every page load.

Call the below query for every page −

mysql> SET time_zone = ' + 0:00';
Query OK, 0 rows affected (0.00 sec)

But it’s not a good practice to call a query every time.

Updated on: 30-Jul-2019

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements