mysql unix_timestamp 时间日期转换时间戳问题

问题:

做AC下发指定时间关闭射频功能时,遇到AC页面配置到数据库时间正确,但是从数据库取出时间转换为时间戳时,转换有问题,时间不正确!

原因:

由于mysql中的时区原因,使用unix_timestamp 转换为时间戳与时区有关!

解决:

设置AC的时区为当前所处位置的时区。

  1. 查看数据库的时区
MariaDB [(none)]> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-07-25 05:22:39 |
+---------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> ;
ERROR: No query specified

MariaDB [(none)]> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-07-25 05:26:16 |
+---------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> 
MariaDB [(none)]> show variables like "%time_zone%";
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | EDT    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)

MariaDB [(none)]> exit

2.查看系统时区

[root@localhost zte]# date
Thu Jul 25 05:48:58 EDT 2019

3.设置系统时区,重启mariadb,查看时区配置
通过timedatectl set-timezone Asia/Shanghai 命令设置系统时区

[root@localhost zte]# timedatectl set-timezone Asia/Shanghai 
[root@localhost zte]# service mariadb restart
Redirecting to /bin/systemctl restart mariadb.service
MariaDB [(none)]> show variables like "%time_zone%";
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | SYSTEM |
+------------------+--------+

4.去除年月日,只需要要时分秒转换为时间戳

MariaDB [(none)]> select time_to_sec('2019-07-25 14:40:00');
+------------------------------------+
| time_to_sec('2019-07-25 14:40:00') |
+------------------------------------+
|                              52800 |
+------------------------------------+

你可能感兴趣的:(AC)