MySQL时间戳和时间的获取/相互转换/格式化


MySQL时间戳和时间的获取/相互转换/格式化

获取当前时间戳

mysql> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
|            1584524789 |
+-----------------------+
1 row in set (0.00 sec)


mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
|       1584524524 |
+------------------+
1 row in set (0.00 sec)

获取当前时间

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2020-03-18 17:39:13 |
+---------------------+
1 row in set (0.00 sec)


mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2020-03-18  |
+-------------+
1 row in set (0.00 sec)

获取三天前的时间

mysql> SELECT NOW() - interval 72 hour;
+--------------------------+
| NOW() - interval 72 hour |
+--------------------------+
| 2020-03-15 17:39:44      |
+--------------------------+
1 row in set (0.00 sec)

时间转时间戳

mysql> select unix_timestamp('2018-01-15 09:45:16');
+---------------------------------------+
| unix_timestamp('2018-01-15 09:45:16') |
+---------------------------------------+
|                            1515980716 |
+---------------------------------------+
1 row in set (0.00 sec)

时间戳转时间

mysql> select from_unixtime(1515980716);
+---------------------------+
| from_unixtime(1515980716) |
+---------------------------+
| 2018-01-15 09:45:16       |
+---------------------------+
1 row in set (0.02 sec)

时间戳格式化

mysql> SELECT from_unixtime(1515980716, '%Y-%m-%d %H:%i:%S');
+------------------------------------------------+
| from_unixtime(1515980716, '%Y-%m-%d %H:%i:%S') |
+------------------------------------------------+
| 2018-01-15 09:45:16                            |
+------------------------------------------------+
1 row in set (0.00 sec)

时间格式化

mysql> select date_format(now(), '%Y-%m-%d');
+--------------------------------+
| date_format(now(), '%Y-%m-%d') |
+--------------------------------+
| 2020-03-18                     |
+--------------------------------+
1 row in set (0.00 sec)

mysql> select date_format('2018-01-15 09:45:16', '%Y-%m-%d');
+------------------------------------------------+
| date_format('2018-01-15 09:45:16', '%Y-%m-%d') |
+------------------------------------------------+
| 2018-01-15                                     |
+------------------------------------------------+
1 row in set (0.00 sec)

函数说明

FROM_UNIXTIME

作用:将MySQL中以INT(11)存储的时间以”YYYY-MM-DD”格式来显示。

语法:FROM_UNIXTIME(unix_timestamp,format)

返回表示 Unix 时间标记的一个字符串,根据format字符串格式化。format可以包含与DATE_FORMAT()函数列出的条目同样的修饰符。

date_format

作用:将MySQL中的日期格式转换成”YYYY-MM-DD”格式来显示。

语法:date_format(now(), ‘%Y-%m-%d’);

  • 根据format字符串格式化date值,下列修饰符可以被用在format字符串中
表达式 含义
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字

文章作者: 阿牛
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 阿牛 !
评论
  目录