Set Time Zone ใน MySql

วันนี้เจอปํญหาการสร้างตารางใน MySql ให้แสดงค่า Time Stamp คือผมทำโปรเจ็คเกี่ยวกับ ESP8266 ให้ส่งค่าที่วัดได้จาก sensor มาเก็บไว้ในตาราง MySql พร้อมกับระบุเวลาจัดเก็บเป็น Time Stamp แต่พอเขียน php ให้แสดงผลข้อมูลแล้วปรากฎว่าเวลาที่ดึงมาจาก ตาราง mySql ไม่ตรงกับเวลาปัจจุบัน ทางแก้แรกที่ผมทำก็คือ บวกค่าเวลาเข้าไปในตัวแปร เช่น

$last_reading_time = date(“Y-m-d H:i:s”, strtotime(“$last_reading_time + 14 hours”));

แต่วิธีนี้ไม่สะดวก จุงหาวิธีเปลี่ยน Time Zone ของ MySql โดยตรง ซึ่งข้อมูลที่หามาได้ดังนี้

ให้เข้าไปแก้ไขไฟล์
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
แล้วเพิ่มคำสั่งนี้เข้าไป default-time-zone='+01:00' ถ้าประเทศไทยใช้ +7

Example:

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

### Default timezone ###
default-time-zone='+01:00'

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.

จากนั้นเวลาที่ Time Stamp ก็จะตรงกับเวลาจริง

Div24Hr.COM
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.