MySQL Current Time Guide (NOW(), TIMESTAMP, Time Zones)

目次

1. Introduction

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.
FunctionReturned ValueExample
NOW()Current date and time (date + time)SELECT NOW();2025-02-11 16:00:00
CURRENT_TIMESTAMPSame as NOW() (SQL standard)SELECT CURRENT_TIMESTAMP;
CURDATE()Current date onlySELECT CURDATE();2025-02-11
CURTIME()Current time onlySELECT 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?

PurposeRecommended Function
Want to retrieve both date and timeNOW() or CURRENT_TIMESTAMP
Want to retrieve only the dateCURDATE()
Want to retrieve only the timeCURTIME()

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.
SELECT DATE_FORMAT(datetime_to_format, 'format_specifier');
Example: NOW() to YYYY/MM/DD HH:MM format
SELECT DATE_FORMAT(NOW(), '%Y/%m/%d %H:%i');
Output:
2025/02/11 16:45

List of format specifiers

SpecifierMeaningExample (2025-02-11 16:45:30)
%Y4-digit year2025
%m2-digit month (01-12)02
%d2-digit day (01-31)11
%HHour in 24-hour format (00-23)16
%iMinutes (00-59)45
%sSeconds (00-59)30

Retrieve only the time part with TIME()

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.
FunctionReturned valueSQLExample output (2025-02-11 16:45:30)
YEAR()YearSELECT YEAR(NOW());2025
MONTH()MonthSELECT MONTH(NOW());2
DAY()DaySELECT 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

Using INTERVAL for addition and subtraction

Basic syntax

SELECT current_time + INTERVAL numeric_value unit;
SELECT current_time - INTERVAL numeric_value unit;

Addition based on NOW()

For example, to get the date and time one week later:
SELECT NOW() + INTERVAL 7 DAY;
Example output:
2025-02-18 16:30:00
UnitMeaningExample
SECONDsecondsNOW() + INTERVAL 10 SECOND
MINUTEminutesNOW() + INTERVAL 5 MINUTE
HOUR>hoursNOW() + INTERVAL 2 HOUR
DAYdaysNOW() + INTERVAL 10 DAY
MONTHmonthsNOW() + INTERVAL 3 MONTH

Using DATEDIFF() to calculate the difference between two dates

SELECT DATEDIFF(NOW(), '2025-01-01');
Example output:
30

Using BETWEEN to filter a date range

SELECT * FROM orders
WHERE created_at BETWEEN '2025-02-01 00:00:00' AND '2025-02-28 23:59:59';

5. MySQL Time Zone Settings

Check the Current Time Zone

SHOW VARIABLES LIKE '%time_zone%';
Example Output:
+------------------+----------------+
| Variable_name    | Value          |
+------------------+----------------+
| system_time_zone | UTC            |
| time_zone       | SYSTEM         |
+------------------+----------------+

Change the Time Zone per Session

SET SESSION time_zone = 'Asia/Tokyo';

Change the Server’s Default Time Zone

Add the following to the configuration file (my.cnf):
[mysqld]
default_time_zone = 'Asia/Tokyo'

Retrieve UTC time using UTC_TIMESTAMP

SELECT UTC_TIMESTAMP();

Convert to local time using CONVERT_TZ()

SELECT CONVERT_TZ(UTC_TIMESTAMP(), 'UTC', 'Asia/Tokyo');

6. How to set the current time as the default value in MySQL

CURRENT_TIMESTAMP set as default value

CREATE TABLE logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Automatic update with ON UPDATE CURRENT_TIMESTAMP

CREATE TABLE logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Difference between DATETIME type and TIMESTAMP type

TypeTimezone impactCURRENT_TIMESTAMP default value setting
TIMESTAMPAffectedPossible
DATETIMENot affectedNot possible

Why NOW() cannot be used as a default value and the solution

ERROR 1067 (42000): Invalid default value for 'created_at'

Solution:

CREATE TRIGGER set_created_at
BEFORE INSERT ON logs
FOR EACH ROW
SET NEW.created_at = NOW();

7. Common MySQL Errors and Solutions (FAQ)

NOW() cannot be used as a default value

Error Example

CREATE TABLE logs (
    created_at DATETIME DEFAULT NOW()
);
ERROR 1067 (42000): Invalid default value for 'created_at'

Solution

CREATE TABLE logs (
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CURRENT_TIMESTAMP time is off

SHOW VARIABLES LIKE 'time_zone';

Solution

SET SESSION time_zone = 'Asia/Tokyo';

NOW() result is off by one hour

SHOW VARIABLES LIKE 'system_time_zone';

Solution

SET GLOBAL time_zone = 'Asia/Tokyo';

BETWEEN range specification does not work correctly

SELECT * FROM orders
WHERE created_at BETWEEN '2025-02-01' AND '2025-02-28';

Solution

SELECT * FROM orders
WHERE created_at BETWEEN '2025-02-01 00:00:00' AND '2025-02-28 23:59:59';

8. Best Practices for Handling Current Time in MySQL

When to use NOW() vs CURRENT_TIMESTAMP

Use caseRecommended
Retrieve current time in a SELECT statementNOW()
Automatically set current time on INSERTCURRENT_TIMESTAMP
Set as default value for TIMESTAMP columnsCURRENT_TIMESTAMP

When to use TIMESTAMP vs DATETIME

Data typeTimezone effectStorage size
TIMESTAMPAffected4 bytes
DATETIMEUnaffected8 bytes

Design: Store in UTC and convert to local time

SELECT CONVERT_TZ(event_time, 'UTC', 'Asia/Tokyo');

Use >= and < instead of BETWEEN

SELECT * FROM orders
WHERE created_at >= '2025-02-01 00:00:00' 
AND created_at < '2025-03-01 00:00:00';

Standardize the use of INTERVAL

SELECT NOW() + INTERVAL 1 DAY;

Clean up data correctly

DELETE FROM logs WHERE created_at < NOW() - INTERVAL 1 YEAR LIMIT 1000;