MySQL获取时间函数

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)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N3verL4nd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值