Home > Database > Mysql Tutorial > body text

What is the difference between current_time/current_date() and now() in Mysql

WBOY
Release: 2023-05-28 14:17:03
forward
1691 people have browsed it

    current_date() only displays the date of the current time

    例如:
    select current_date() from a ;
    结果:2021-08-25
    Copy after login

    current_time() only displays the hours, minutes and seconds of the current time

    例如:
    select CURRENT_TIME() from a ;
    14:07:06
    Copy after login

    now() displays all

    例如:
    select now() from a ;
    结果:2021-08-25 14:07:56
    Copy after login

    When increasing or decreasing dates current_time, current_date() is similar to now() and the details are as follows: CURRENT_DATE() function

    #获取当前日期
    SELECT CURRENT_DATE() AS newDate;    #2021-05-20
    #当前日期加1天(其他天数方法一样)
    SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY) AS newDate;    #2021-05-21
    #当前日期减1天
    SELECT DATE_ADD(CURRENT_DATE(), INTERVAL -1 DAY) AS newDate;   #2021-05-19
    #当前日期加1个月
    SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH) AS newDate;  #2021-06-20
    #当前日期减1个月
    SELECT DATE_ADD(CURRENT_DATE(), INTERVAL -1 MONTH) AS newDate; #2021-04-20
    #当前日期加1年
    SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 1 YEAR) AS newDate;   #2022-05-20
    #当前日期减1年
    SELECT DATE_ADD(CURRENT_DATE(), INTERVAL -1 YEAR) AS newDate;  #2020-05-20
    
    #指定日期加1天(可将DAY换为MONTH,YEAR来实现加1个月,1年)
    SELECT DATE_ADD('2021-05-20', INTERVAL 1 DAY) AS newDate;    #2021-05-21
    #指定日期减1天(可将DAY换为MONTH,YEAR来实现减1个月,1年)
    SELECT DATE_ADD('2021-05-20', INTERVAL -1 DAY) AS newDate;   #2021-05-19
    
    #当前时间添加1小时10分钟10秒钟
    SELECT DATE_ADD(NOW(), INTERVAL '1:10:10' HOUR_SECOND) AS newTime;    #2021-05-20 15:59:32
    #指定时间添加1小时10分钟10秒钟
    SELECT DATE_ADD('2019-04-17 2:00:00', INTERVAL '1:10:10' HOUR_SECOND) AS newTime;    #2021-04-17 03:10:10
    Copy after login

    NOW() function

    #获取当前时间
    SELECT NOW() AS newTime;    #2021-05-20 14:28:41
    #当前时间减30秒
    SELECT (NOW() - INTERVAL 30 SECOND) AS newTime;    #2021-05-20 14:29:31
    #当前时间加30秒
    SELECT (NOW() + INTERVAL 30 SECOND) AS newTime;    #2021-05-20 14:29:31
    #当前时间减30分钟
    SELECT (NOW() - INTERVAL 30 MINUTE) AS newTime;    #2021-05-20 14:02:26
    #当前时间加30分钟
    SELECT (NOW() + INTERVAL 30 MINUTE) AS newTime;    #2021-05-20 15:02:41
    #当前时间减1天
    SELECT (NOW() - INTERVAL 1 DAY) AS newTime;        #2021-05-19 14:33:26
    #当前时间加1天
    SELECT (NOW() + INTERVAL 1 DAY) AS newTime;        #2021-05-21 14:33:33
    #当前时间减1个月
    SELECT (NOW() - INTERVAL 1 MONTH) AS newTime;      #2021-04-20 14:34:10
    #当前时间加1个月
    SELECT (NOW() + INTERVAL 1 MONTH) AS newTime;      #2021-06-20 14:34:47
    #当前时间减1年
    SELECT (NOW() - INTERVAL 1 YEAR) AS newTime;       #2020-05-20 14:35:09
    #当前时间加1年
    SELECT (NOW() + INTERVAL 1 MONTH) AS newTime;      #2022-05-20 14:35:23
    
    #指定时间的加减,将上面的NOW()函数换为指定日期时间即可,以加30分钟为例,如下:
    SELECT ('2021-05-20 12:30:00' + INTERVAL 30 MINUTE) AS newTime;        #2021-05-20 13:00:00
    Copy after login

    The above is the detailed content of What is the difference between current_time/current_date() and now() in Mysql. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template