You get a bonus - 1 coin for daily activity. Now you have 1 coin

Setting the correct time zone in php and mysql. Time Zones in MYSQL

Lecture



  Setting the correct time zone in php and mysql.  Time Zones in MYSQL

The time zone should be the same in MYSQL and PHP if it is installed on the same server

Setting the time zone in mysql from php is done as follows:

 
query( "SET time_zone = 'Europe/Kiev'" );

or by request

- SET GLOBAL time_zone = 'Europe / Kiev';

 SET GLOBAL time_zone = '+2:00'; 
 SET time_zone='EST5EDT'; 

To use the system time in MySQL you need to run

 
 
SET GLOBAL time_zone = 'SYSTEM' ;
SET time_zone = 'SYSTEM' ;
 

Setting the time zone in php for date and time functions:

one
2
if (function_exists( 'date_default_timezone_set' ))
date_default_timezone_set( 'Europe/Kiev' );

In belt zones:


Europe / Kiev
Europe / London
Europe / Minsk
Europe / Moscow
Europe / Riga
Europe / Samara
Europe / Simferopol
Europe / Tallinn
Europe / Tiraspol
Europe / Uzhgorod
Europe / Volgograd
Asia / Yekaterinburg
Asia / Novosibirsk
Asia / Krasnoyarsk
Asia / Irkutsk
Asia / Kamchatka
Asia / Magadan
Asia / Sakhalin

If you need to permanently set a time zone, then you need to change the configuration files:

- in .htaccess or httpd.conf should add a line like
php_value date.timezone "Europe / Kiev"

- in php.ini a line of the form
date.timezone = Europe / Kiev

For mysql:

You can add the timezone in the configuration file in the following way (in this case, a reboot is required):

/etc/my.cnf

-zone = time -zone = "Europe/Kiev"

You can change the time without rebooting. To do this, first transfer the system time zones to MySQL in the following way:

mysql_tzinfo_to_sql /usr/share/zoneinfo |mysql -u root mysql -p

then you need to enter the password of the user MySql root

Further, we can already update the time zone without the appearance of errors like:

ERROR 1298 (HY000): Unknown or incorrect time zone:

Perform the update time_zone:

 
 
SET GLOBAL time_zone = 'Europe/Kiev' ;
SET time_zone = 'Europe/Kiev' ;

In MySQL, you can also use the system time, it is probably even better. To change the current system time on the server, you need to do:

one
cp /usr/share/zoneinfo/Europe/Kiev /etc/localtime

To use the system time in MySQL you need to run

one
2
SET GLOBAL time_zone = 'SYSTEM' ;
SET time_zone = 'SYSTEM' ;

Get time zone data in MYSQL

 SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP); 

To check the current time zone, run the command:

 
 
SHOW VARIABLES LIKE '%zone%' ;
SELECT @@global.time_zone, @@session.time_zone;

To view the current MySQL server time:

select current_timestamp();

Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Databases - MySql (Maria DB)

Terms: Databases - MySql (Maria DB)