How to use the MySQL CURDATE() function
The CURDATE()
function in MySQL returns the current date in ‘YYYY-MM-DD’ format. It is useful for inserting and comparing against the current date.
The CURDATE()
function in MySQL returns the current date in ‘YYYY-MM-DD’ format. It is useful for inserting and comparing against the current date.
Syntax
The syntax for CURDATE()
is simple:
CURDATE()
It takes no arguments.
Examples
-
Insert current date into a table:
INSERT INTO purchases(purchase_date) VALUES (CURDATE());
This inserts the current date into the purchase_date column.
-
Select records where date field is current date:
SELECT * FROM appointments WHERE appt_date = CURDATE();
This returns records where the appointment date is today’s date.
-
Compare current date to a specified date:
SELECT CURDATE() = '2023-11-09';
This compares the current date to the specified date.
-
Use CURRENT_DATE as alias for CURDATE():
SELECT CURRENT_DATE();
Both CURRENT_DATE() and CURDATE() return the current date.
-
Get the current date and time together:
SELECT CONCAT(CURDATE(), ' ', CURTIME());
This returns current date and time together.
Other Similar Functions
CURRENT_DATE()
- Alias forCURDATE()
NOW()
- Current date and timeCURTIME()
- Current timeUNIX_TIMESTAMP()
- Unix timestamp
So CURDATE()
provides an easy way to use the current date in queries, inserts, and comparisons in MySQL.