Home > Database > Mysql Tutorial > How to Work with Time Zones in MySQL?

How to Work with Time Zones in MySQL?

Barbara Streisand
Release: 2024-11-03 19:59:29
Original
452 people have browsed it

How to Work with Time Zones in MySQL?

MySQL Time Zones

MySQL provides support for a wide range of time zones, enabling you to store and manipulate dates and times in various geographical regions.

List of Time Zones

MySQL >= 5.7

To view an exhaustive list of MySQL time zones, execute the following query:

<code class="sql">SELECT * FROM mysql.time_zone_name;</code>
Copy after login

MySQL < 5.7

To load the time zone data from the system and view the list, run these commands:

<code class="bash">mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Copy after login
Copy after login
<code class="sql">USE mysql;
SELECT * FROM `time_zone_name`;
Copy after login

Setting Calgary Local Time

To display time in Calgary local time, you can set the time_zone system variable:

<code class="sql">SET time_zone = 'America/Edmonton';
Copy after login

Note: Replace 'America/Edmonton' with the appropriate time zone identifier for Calgary.

Troubleshooting

If you find that time zones are not available in MySQL, execute the following command:

<code class="bash">mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Copy after login
Copy after login

This command loads the system time zone data into MySQL.

Regular Updates

To ensure that MySQL time zones are automatically updated when the system time zones change, add the following script to a cron job:

#!/bin/bash

if [ `find /usr/share/zoneinfo -mtime -1 | grep -v '\.tab' | wc -l` -gt 0 ]; then
    echo "Updating MySQL timezone info"
    mysql_tzinfo_to_sql /usr/share/zoneinfo 2>/dev/null | mysql -u root mysql
    echo "Done!"
fi

The above is the detailed content of How to Work with Time Zones in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template