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 |
+---------------------+---------------------+

2. 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'

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('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)之间相差的天数。

最新文章

  1. Win7下清除SQL SERVER 2008的SSMS保存的登录信息
  2. C#委托,事件理解入门 (译稿)
  3. manacher浅析
  4. 从此爱上iOS Autolayout
  5. javascript设计模式-观察者模式
  6. ubuntu 安装sublime并激活
  7. 【转载】从头编写 asp.net core 2.0 web api 基础框架 (3)
  8. Perf工具
  9. What's the meaning of unqualified-id?
  10. FQ原理
  11. npm包
  12. webstorm 设置 sass自动编译问题
  13. 一篇好文之Android数据库 GreenDao的完全解析
  14. linux find 10天内改动过的文件
  15. HDU 2460 Network(桥+LCA)
  16. 更新403 Forbidden
  17. netcore如何引用package?
  18. qt离线下载
  19. 通过开机广播(broadcast)通知应用
  20. 编程之美 set 16 拈游戏分析(1)

热门文章

  1. Bzoj 1046: [HAOI2007]上升序列 二分,递推
  2. [ZETCODE]wxWidgets教程九:组件专题2
  3. 《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的分布
  4. 微信、QQ这类IM App怎么做——谈谈Websocket
  5. 1 weekend110的hdfs源码跟踪之打开输入流 + hdfs源码跟踪之打开输入流总结
  6. KindEditor编辑器(初始化参数)
  7. SSH开源框架考试题
  8. PAT---1050. String Subtraction (20)
  9. 【ThinkingInC++】65、使用delete void*可能会出错
  10. android之TabWidget选项卡