In mysql, you can use the CURDATE() and CURRENT_DATE() functions to get the current date. You can return the current date in the format of "YYYY-MM-DD" or "YYYYMMDD". The specific format is based on the function. Depends on whether used in string or numeric context.
(Recommended tutorial: mysql video tutorial)
CURDATE() and CURRENT_DATE() in MySQL
The CURDATE() and CURRENT_DATE() functions in MySQL have the same function. They return the current date in the format of "YYYY-MM-DD" or "YYYYMMDD". The specific format depends on the characters used in the function. Depends on string or number context.
[Example 1] Use the date functions CURDATE and CURRENT_DATE to obtain the current date of the system. The input SQL statement and execution results are as follows.
mysql> SELECT CURDATE(),CURRENT_DATE(),CURRENT_DATE()+0; +------------+----------------+------------------+ | CURDATE() | CURRENT_DATE() | CURRENT_DATE()+0 | +------------+----------------+------------------+ | 2020-10-13 | 2020-10-13 | 20201013 | +------------+----------------+------------------+ 1 row in set (0.03 sec)
It can be seen from the running results that the two functions have the same effect and return the same current date of the system. "CURDATE() 0" converts the current date value into a numerical value.
NOW() and SYSDATE() in MySQL
The NOW() and SYSDATE() functions in MySQL have the same function. They both return the current date and time value. The format is "YYYY-MM-DD HH:MM:SS" or "YYYYMMDDHHMMSS", depending on whether the function is used in a string or numeric context.
[Example 2] Use the date and time functions NOW and SYSDATE to obtain the date and time of the current system. The input SQL statement and execution results are as follows.
mysql> SELECT NOW(),SYSDATE(); +---------------------+---------------------+ | NOW() | SYSDATE() | +---------------------+---------------------+ | 2017-04-01 19:36:52 | 2017-04-01 19:36:52 | +---------------------+---------------------+ 1 row in set (0.04 sec)
It can be seen from the running results that the results returned by the NOW function and the SYSDATE function are the same.
Although NOW() and SYSDATE() both represent the current time in MySQL, NOW() takes the time when the statement starts executing, while SYSDATE() takes the dynamic real-time time during the execution of the statement. .
[Example 3] First queried NOW() and SYSDATE(), then slept for 3 seconds, and then queried NOW() and SYSDATE(). The results are as follows:
mysql> select now(),sysdate(),sleep(3),now(),sysdate(); +-----------------------+------------------------+-------------+-----------------------+---------------------+ | now() | sysdate() | sleep(3) | now() | sysdate() | +-----------------------+------------------------+-------------+------------------- ---+---------------------+ | 2019-02-27 10:59:39 | 2019-02-27 10:59:39 | 0 | 2019-02-27 10:59:39 | 2019-02-27 10:59:42 | +-----------------------+------------------------+-------------+-----------------------+---------------------+ 1 row in set (3.00 sec)
By running results It can be seen that the NOW() function always obtains the time when the SQL statement starts executing, while the SYSDATE() function obtains the real-time time dynamically.
The above is the detailed content of How to get the current date in mysql? What are the date functions?. For more information, please follow other related articles on the PHP Chinese website!