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

MySQLMySQLi Database

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.

raja
Published on 11-Jan-2019 10:05:33
Advertisements