Memo

メモ > 技術 > データベース: MySQL > 日時を扱う

日時を扱う
CURRENT_DATE() で日付を、CURRENT_TIME() で時間を、NOW() で日時をそれぞれ取得できる 時間の計算を行う際、CURRENT_DATE() を使用すると0時からの計算になるので注意
MySQL [recole]> SELECT CURRENT_DATE(); +----------------+ | CURRENT_DATE() | +----------------+ | 2022-11-08 | +----------------+ 1 row in set (0.00 sec) MySQL [recole]> SELECT CURRENT_TIME(); +----------------+ | CURRENT_TIME() | +----------------+ | 12:59:14 | +----------------+ 1 row in set (0.00 sec) MySQL [recole]> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2022-11-08 12:59:14 | +---------------------+ 1 row in set (0.00 sec) MySQL [recole]> SELECT CURRENT_DATE(), DATE_ADD(CURRENT_DATE(), INTERVAL 13 HOUR); +----------------+--------------------------------------------+ | CURRENT_DATE() | DATE_ADD(CURRENT_DATE(), INTERVAL 13 HOUR) | +----------------+--------------------------------------------+ | 2022-11-08 | 2022-11-08 13:00:00 | +----------------+--------------------------------------------+ 1 row in set (0.00 sec) MySQL [recole]> SELECT NOW(), DATE_ADD(NOW(), INTERVAL 13 HOUR); +---------------------+-----------------------------------+ | NOW() | DATE_ADD(NOW(), INTERVAL 13 HOUR) | +---------------------+-----------------------------------+ | 2022-11-08 12:59:14 | 2022-11-09 01:59:14 | +---------------------+-----------------------------------+ 1 row in set (0.00 sec)

Advertisement