MySQL获取时间函数

来源:互联网 发布:魔兽世界单机版数据库 编辑:程序博客网 时间:2024/05/24 01:46
1、 获得当前日期+时间(date + time)函数:now()
mysql> SELECT now();+---------------------+| now()               |+---------------------+| 2017-06-15 09:16:56 |+---------------------+1 row in set (0.00 sec)

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

mysql> SELECT sysdate();+---------------------+| sysdate()           |+---------------------+| 2017-06-15 09:17:17 |+---------------------+1 row in set (0.00 sec)
sysdate() 日期时间函数跟和now() 返回的数据格式一样,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。

测试:

mysql> SELECT now(), sleep(5), now();+---------------------+----------+---------------------+| now()               | sleep(5) | now()               |+---------------------+----------+---------------------+| 2017-06-15 09:20:59 |        0 | 2017-06-15 09:20:59 |+---------------------+----------+---------------------+1 row in set (5.11 sec)
其返回的两个值是一样的,因为都是表示语句开始执行的时间。
mysql> SELECT sysdate(), sleep(5), sysdate();+---------------------+----------+---------------------+| sysdate()           | sleep(5) | sysdate()           |+---------------------+----------+---------------------+| 2017-06-15 09:21:43 |        0 | 2017-06-15 09:21:48 |+---------------------+----------+---------------------+1 row in set (5.00 sec)

3 、获得当前日期(date)函数:curdate() 等同于:current_date(),current_date

mysql> SELECT curdate(), current_date(), current_date;+------------+----------------+--------------+| curdate()  | current_date() | current_date |+------------+----------------+--------------+| 2017-06-15 | 2017-06-15     | 2017-06-15   |+------------+----------------+--------------+1 row in set (0.00 sec)
4、获得当前时间(time)函数:curtime()

等同于:current_time(),current_time

mysql> SELECT curtime(), current_time(), current_time;+-----------+----------------+--------------+| curtime() | current_time() | current_time |+-----------+----------------+--------------+| 09:28:11  | 09:28:11       | 09:28:11     |+-----------+----------------+--------------+1 row in set (0.00 sec)

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

因为我国位于东八时区,所以本地时间 = UTC 时间 + 8 小时。

mysql> SELECT utc_date(), utc_time(), utc_timestamp();+------------+------------+---------------------+| utc_date() | utc_time() | utc_timestamp()     |+------------+------------+---------------------+| 2017-06-15 | 01:28:52   | 2017-06-15 01:28:52 |+------------+------------+---------------------+1 row in set (0.00 sec)
6、获得Unix系统中的时间戳

mysql> SELECT unix_timestamp();+------------------+| unix_timestamp() |+------------------+|       1497490186 |+------------------+1 row in set (0.00 sec)



原创粉丝点击