ラボ > MySQL:日時関連

Mysql unixtimeとdatetime型の相互変換

日時をunixtimeで格納しているけど、datetime型で表示したい。ついでに日時の書式変換も。

作成日:2018-01-22, 更新日:2019-05-17

unixtime→datetime型

unixtimeを「2018-01-22 11:40:30」のようなdatetime型に変換。

select from_unixtime(〇〇〇);

書式変換

▼datetime型にしてから、date_format()で囲む。

select date_format(from_unixtime(〇〇〇), "%Y-%m-%d");

datetime型→unixtime

「2018-01-22 11:40:30」のようなdatetime型をunixtimeに変換。

select unix_timestamp(〇〇〇);

現在の日時の取得

今のところ使う予定は無いけど、メモ。

datetime型

select now();

unixtime

現在の日時(datetime型)をunixtimeに変換。

select unix_timestamp(now());