0
0
MySQLquery~5 mins

NOW, CURDATE, CURTIME in MySQL

Choose your learning style9 modes available
Introduction
These functions help you get the current date and time from the database server easily.
When you want to record the exact time a user submits a form.
When you need to filter records created today.
When you want to display the current time on a dashboard.
When you want to compare a stored date with the current date.
When you want to log events with timestamps automatically.
Syntax
MySQL
NOW()
CURDATE()
CURTIME()
NOW() returns the current date and time together.
CURDATE() returns only the current date (year-month-day).
CURTIME() returns only the current time (hour:minute:second).
Examples
Gets the current date and time, like '2024-06-15 14:30:45'.
MySQL
SELECT NOW();
Gets only the current date, like '2024-06-15'.
MySQL
SELECT CURDATE();
Gets only the current time, like '14:30:45'.
MySQL
SELECT CURTIME();
Gets all three values in one query.
MySQL
SELECT NOW(), CURDATE(), CURTIME();
Sample Program
This query shows the current date and time, the date only, and the time only in separate columns.
MySQL
SELECT NOW() AS current_datetime, CURDATE() AS current_date, CURTIME() AS current_time;
OutputSuccess
Important Notes
The exact output depends on the database server's clock and timezone.
These functions are useful for timestamps without needing to pass values from your application.
Use these functions to keep your data consistent with the server time.
Summary
NOW() returns the full current date and time.
CURDATE() returns only the current date.
CURTIME() returns only the current time.