1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > mysql 获取时间戳函数_MySQL 时间戳(Timestamp)函数

mysql 获取时间戳函数_MySQL 时间戳(Timestamp)函数

时间:2023-02-26 16:39:23

相关推荐

mysql 获取时间戳函数_MySQL 时间戳(Timestamp)函数

1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()

mysql> select current_timestamp,

current_timestamp();

+---------------------+---------------------+

| current_timestamp | current_timestamp() |

+---------------------+---------------------+

| -08-09 23:22:24 | -08-09 23:22:24 |

+---------------------+---------------------+

2. MySQL (Unix 时间戳、日期)转换函数:

unix_timestamp(),

unix_timestamp(date),

from_unixtime(unix_timestamp),

from_unixtime(unix_timestamp,format)

下面是示例:

select unix_timestamp(); -- 1218290027

select unix_timestamp('-08-08'); -- 1218124800

select unix_timestamp('-08-08 12:30:00'); -- 1218169800

select from_unixtime(1218290027); -- '-08-09 21:53:47'

select from_unixtime(1218124800); -- '-08-08 00:00:00'

select from_unixtime(1218169800); -- '-08-08 12:30:00'

select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '

8th August 12:30:00 '

3. MySQL 时间戳(timestamp)转换、增、减函数:

timestamp(date) -- date to timestamp

timestamp(dt,time) -- dt + time

timestampadd(unit,interval,datetime_expr) --

timestampdiff(unit,datetime_expr1,datetime_expr2) --

请看示例部分:

select timestamp('-08-08'); -- -08-08 00:00:00

select timestamp('-08-08 08:00:00', '01:01:01'); -- -08-08

09:01:01

select timestamp('-08-08 08:00:00', '10 01:01:01'); --

-08-18 09:01:01

select timestampadd(day, 1, '-08-08 08:00:00'); -- -08-09

08:00:00

select date_add('-08-08 08:00:00', interval 1 day); --

-08-09 08:00:00

MySQL timestampadd() 函数类似于 date_add()。

select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1

select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485

select timestampdiff(hour,'-08-08 12:00:00','-08-08

00:00:00'); -- -12

select datediff('-08-08 12:00:00', '-08-01 00:00:00'); --

7

MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff()

只能计算两个日期(date)之间相差的天数。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。