What are the cases for handling the current time in MySQL?
In MySQL, retrieving the current time is needed in various situations. For example, the following use cases can be considered.
Automatic timestamp entry when registering data
For instance, when saving order data or log data, record the creation date and time of the data.
Filtering data based on the current time
For example, retrieving only data from the past 7 days or searching for data with future dates.
Manipulating and displaying dates and times
When creating reports, format the date and time for better readability.
Managing data expiration using the current time
For example, determine a coupon’s expiration by comparing it with the current time.
Thus, properly retrieving and manipulating the current time in MySQL is an essential skill for database operations.
What you’ll learn in this article
In this article, we will explain the following topics in detail.
How to retrieve the current time in MySQL (NOW(), CURRENT_TIMESTAMP, etc.)
Changing date and time formats (how to use DATE_FORMAT())
Date and time calculations using the current time (date manipulation with INTERVAL)
How to change the time zone (SET SESSION time_zone)
Using the current time as a default value (leveraging CURRENT_TIMESTAMP)
Common error causes and solutions (FAQ)
We will cover everything from the basics to advanced usage of handling the “current time” with MySQL, including practical SQL examples, so please read through to the end.
2. How to Get the Current Time in MySQL
List of Functions to Retrieve the Current Time in MySQL
MySQL provides several functions to obtain the current time. Understand their differences and use them appropriately.
Function
Returned Value
Example
NOW()
Current date and time (date + time)
SELECT NOW(); → 2025-02-11 16:00:00
CURRENT_TIMESTAMP
Same as NOW() (SQL standard)
SELECT CURRENT_TIMESTAMP;
CURDATE()
Current date only
SELECT CURDATE(); → 2025-02-11
CURTIME()
Current time only
SELECT CURTIME(); → 16:00:00
Using the NOW() Function
NOW() is the most common function in MySQL for retrieving the current time. It returns both date and time.
SELECT NOW();
Example output:
2025-02-11 16:00:00
NOW() returns the current system time.
Because it is affected by the time zone, the displayed time may differ from expectations depending on the environment (see the “Time Zone Settings” section for details).
How to Use CURRENT_TIMESTAMP
CURRENT_TIMESTAMP works almost the same as NOW(). It conforms to the SQL standard and can be used similarly in other databases.
SELECT CURRENT_TIMESTAMP;
Example output:
2025-02-11 16:00:00
Getting Only the Date with CURDATE()
CURDATE() is used when you want to retrieve only the current date (year-month-day).
SELECT CURDATE();
Example output:
2025-02-11
Getting Only the Time with CURTIME()
Use CURTIME() when you want to retrieve only the current time (hours, minutes, seconds).
SELECT CURTIME();
Example output:
16:00:00
Which Function Should You Use?
Purpose
Recommended Function
Want to retrieve both date and time
NOW() or CURRENT_TIMESTAMP
Want to retrieve only the date
CURDATE()
Want to retrieve only the time
CURTIME()
3. How to format the current time in MySQL
DATE_FORMAT() Custom Format
DATE_FORMAT() Basic Syntax
In MySQL, you can use the DATE_FORMAT() function to freely change the format of dates and times.
If you want to extract only the time part from the datetime obtained with NOW(), use the TIME() function.
SELECT TIME(NOW());
Output:
16:45:30
Partial extraction with YEAR(), MONTH(), DAY()
To extract specific parts, use the following functions.
Function
Returned value
SQL
Example output (2025-02-11 16:45:30)
YEAR()
Year
SELECT YEAR(NOW());
2025
MONTH()
Month
SELECT MONTH(NOW());
2
DAY()
Day
SELECT DAY(NOW());
11
Practical examples of format changes
The following SQL is handy for trying out various formats.
SELECT
NOW() AS 'Original datetime',
DATE_FORMAT(NOW(), '%Y/%m/%d') AS 'YYYY/MM/DD format',
DATE_FORMAT(NOW(), '%H:%i:%s') AS 'Hour:Minute:Second',
TIME(NOW()) AS 'Time only',
YEAR(NOW()) AS 'Year',
MONTH(NOW()) AS 'Month',
DAY(NOW()) AS 'Day';
4. Date and time calculations using the current time in MySQL