mysql时间函数

一、MySQL 获得当前日期时间 函数 
1.1 获得当前日期+时间(date + time)函数:now()

mysql> select now();

+---------------------+ 
| now()               | 
+---------------------+ 
| 2008-08-08 22:20:46 | 
+---------------------+

 

1.2 获得当前日期+时间(date + time)函数:sysdate()

sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。看下面的例子就明白了:

mysql> select now(), sleep(3), now();

+---------------------+----------+---------------------+ 
| now()               | sleep(3) | now()               | 
+---------------------+----------+---------------------+ 
| 2008-08-08 22:28:21 |        0 | 2008-08-08 22:28:21 | 
+---------------------+----------+---------------------+

mysql> select sysdate(), sleep(3), sysdate();

+---------------------+----------+---------------------+ 
| sysdate()           | sleep(3) | sysdate()           | 
+---------------------+----------+---------------------+ 
| 2008-08-08 22:28:41 |        0 | 2008-08-08 22:28:44 | 
+---------------------+----------+---------------------+

可以看到,虽然中途 sleep 3 秒,但 now() 函数两次的时间值是相同的; sysdate() 函数两次得到的时间值相差 3 秒。MySQL Manual 中是这样描述 sysdate() 的:Return the time at which the function executes。

sysdate() 日期时间函数,一般情况下很少用到。

 


2. 获得当前日期(date)函数:curdate()

mysql> select curdate();

+------------+ 
| curdate() | 
+------------+ 
| 2008-08-08 | 
+------------+

其中,下面的两个日期函数等同于 curdate():

current_date() 
,current_date

3. 获得当前时间(time)函数:curtime()

mysql> select curtime();

+-----------+ 
| curtime() | 
+-----------+ 
| 22:41:30 | 
+-----------+

其中,下面的两个时间函数等同于 curtime():

current_time() 
,current_time

 

4. 获得当前 UTC 日期时间函数:utc_date(), utc_time(), utc_timestamp()

mysql> select utc_timestamp(), utc_date(), utc_time(), now()

+---------------------+------------+------------+---------------------+ 
| utc_timestamp()     | utc_date() | utc_time() | now()               | 
+---------------------+------------+------------+---------------------+ 
| 2008-08-08 14:47:11 | 2008-08-08 | 14:47:11   | 2008-08-08 22:47:11 | 
+---------------------+------------+------------+---------------------+

因为我国位于东八时区,所以本地时间 = UTC 时间 + 8 小时。UTC 时间在业务涉及多个国家和地区的时候,非常有用。

 

5.

TIMESTAMP值可以从1970的某时的开始一直到2037年,精度为一秒,其值作为数字显示。
TIMESTAMP值显示尺寸的格式如下表所示:

+---------------+----------------+
| 列类型    | 显示格式    |
| TIMESTAMP(14) | YYYYMMDDHHMMSS | 
| TIMESTAMP(12) | YYMMDDHHMMSS  |
| TIMESTAMP(10) | YYMMDDHHMM   |
| TIMESTAMP(8) | YYYYMMDD    |
| TIMESTAMP(6) | YYMMDD     |
| TIMESTAMP(4) | YYMM      |
| TIMESTAMP(2) | YY       |
+---------------+----------------+

“完整”TIMESTAMP格式是14位,但TIMESTAMP列也可以用更短的显示尺寸,创造最常见的显示尺寸是6、8、12、和14。
你可以在创建表时指定一个任意的显示尺寸,但是定义列长为0或比14大均会被强制定义为列长14。
列长在从1~13范围的奇数值尺寸均被强制为下一个更大的偶数。

 

6.

select timediff('2000:01:31 23:59:59', '2000:01:01 00:00:00'); -- 743:59:59 
select timediff('2000:01:01 00:00:00', '2000:01:31 23:59:59'); -- -743:59:59 
select timediff('23:59:59', '12:00:00');                        -- 11:59:59

注意,timediff 的两个参数只能是 datetime/timestamp, time 类型的,并且这两个参数类型要相同。即:datetime/timestamp 和 datetime/timestamp 比较;time 和 time 相比较。

 

 

 

 

 

 

 

 

 

 

 

posted @ 2016-03-16 10:45  Hsinwang  阅读(256)  评论(0编辑  收藏  举报