MySQL 时间戳(Timestamp)函数

http://blog.sina.com.cn/s/blog_645e12550100o6yt.html

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

+———————+———————+
| current_timestamp | current_timestamp() |
+———————+———————+
| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |
+———————+———————+

  1. MySQL (Unix 时间戳、日期)转换函数:
    unix_timestamp(),
    unix_timestamp(date),
    from_unixtime(unix_timestamp),
    from_unixtime(unix_timestamp,format)

下面是示例:
select unix_timestamp(); – 1218290027
select unix_timestamp(‘2008-08-08’); – 1218124800
select unix_timestamp(‘2008-08-08 12:30:00’); – 1218169800

select from_unixtime(1218290027); – ‘2008-08-09 21:53:47’
select from_unixtime(1218124800); – ‘2008-08-08 00:00:00’
select from_unixtime(1218169800); – ‘2008-08-08 12:30:00’

select from_unixtime(1218169800, ‘%Y %D %M %h:%i:%s %x’); – ‘2008 8th August 12:30:00 2008’

  1. 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(‘2008-08-08’); – 2008-08-08 00:00:00
select timestamp(‘2008-08-08 08:00:00’, ‘01:01:01’); – 2008-08-08 09:01:01
select timestamp(‘2008-08-08 08:00:00’, ‘10 01:01:01’); – 2008-08-18 09:01:01

select timestampadd(day, 1, ‘2008-08-08 08:00:00’); – 2008-08-09 08:00:00
select date_add(‘2008-08-08 08:00:00’, interval 1 day); – 2008-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,’2008-08-08 12:00:00’,’2008-08-08 00:00:00’); – -12

select datediff(‘2008-08-08 12:00:00’, ‘2008-08-01 00:00:00’); – 7

MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值