MySQL内置函数——关于时间戳转换和计算函数

it2022-05-05  114

MySQL内置关于时间戳的计算和转换函数介绍

TIMESTAMP函数

语法:

TIMESTAMP(datetime_expr) TIMESTAMP(datetime_expr,expr)

说明: 一个参数时,是将datetime_expr转换成时间戳格式(timestamp),两个参数时,是将datetime_expr转换成时间戳并加上expr

使用举例:

mysql> select timestamp('2019-07-20 08:00:00'); +----------------------------------+ | timestamp('2019-07-20 08:00:00') | +----------------------------------+ | 2019-07-20 08:00:00 | +----------------------------------+ 1 row in set mysql> select timestamp('2019-07-20 08:00:00', '01'); +----------------------------------------+ | timestamp('2019-07-20 08:00:00', '01') | +----------------------------------------+ | 2019-07-20 08:00:01 | +----------------------------------------+ 1 row in set mysql> select timestamp('2019-07-20 08:00:00', '01:01'); +-------------------------------------------+ | timestamp('2019-07-20 08:00:00', '01:01') | +-------------------------------------------+ | 2019-07-20 09:01:00 | +-------------------------------------------+ 1 row in set mysql> select timestamp('2019-07-20 08:00:00', '01:01:01'); +----------------------------------------------+ | timestamp('2019-07-20 08:00:00', '01:01:01') | +----------------------------------------------+ | 2019-07-20 09:01:01 | +----------------------------------------------+ 1 row in set mysql> select timestamp('2019-07-20 08:00:00', '10 01:01:01'); +-------------------------------------------------+ | timestamp('2019-07-20 08:00:00', '10 01:01:01') | +-------------------------------------------------+ | 2019-07-30 09:01:01 | +-------------------------------------------------+ 1 row in set mysql> select timestamp('2019-07-20 08:00:00', '01 10'); +-------------------------------------------+ | timestamp('2019-07-20 08:00:00', '01 10') | +-------------------------------------------+ | 2019-07-21 18:00:00 | +-------------------------------------------+ 1 row in set

TIMESTAMPDIFF函数

语法:

TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)

说明: 返回日期或日期时间表达式datetime_expr1 和datetime_expr2 之间的整数差。其结果的单位由interval 参数给出。该参数必须是以下值的其中一个:

FRAC_SECOND 表示间隔是毫秒 SECOND 秒 MINUTE 分钟 HOUR 小时 DAY 天 WEEK 星期 MONTH 月 QUARTER 季度 YEAR 年

使用如下:

mysql> select TIMESTAMPDIFF(day,'2019-07-10','2019-07-20'); +----------------------------------------------+ | TIMESTAMPDIFF(day,'2019-07-10','2019-07-20') | +----------------------------------------------+ | 10 | +----------------------------------------------+ 1 row in set mysql> select TIMESTAMPDIFF(MINUTE,'2019-07-15 09:00:00','2019-07-20 19:00:00'); +-------------------------------------------------------------------+ | TIMESTAMPDIFF(MINUTE,'2019-07-15 09:00:00','2019-07-20 19:00:00') | +-------------------------------------------------------------------+ | 7800 | +-------------------------------------------------------------------+ 1 row in set

TIMESTAMPADD函数

语法:

TIMESTAMPADD(interval,int_expr,datetime_expr)

说明: 将整型表达式int_expr添加到日期或日期时间表达式 datetime_expr中。式中的nterval和上文中列举的取值是一样的。

例如,int_expr为MINUTE时,是将datetime_expr时间增加int_expr分钟。

mysql> select TIMESTAMPADD(MINUTE,60,'2019-07-20 21:00:00'); +-----------------------------------------------+ | TIMESTAMPADD(MINUTE,60,'2019-07-20 21:00:00') | +-----------------------------------------------+ | 2019-07-20 22:00:00 | +-----------------------------------------------+ 1 row in set

最新回复(0)