Database Reference
In-Depth Information
Discussion
Some applications must know the current date or time, such as those that write time‐
stamped log records. This kind of information is also useful for date calculations per‐
formed in relation to the current date, such as finding the first (or last) day of the month,
or determining the date for Wednesday of next week.
The
CURDATE()
and
CURTIME()
functions return the current date and time separately,
and
NOW()
returns both as a combined date-and-time value:
mysql>
SELECT CURDATE(), CURTIME(), NOW();
+------------+-----------+---------------------+
| CURDATE() | CURTIME() | NOW() |
+------------+-----------+---------------------+
| 2014-02-20 | 18:06:45 | 2014-02-20 18:06:45 |
+------------+-----------+---------------------+
CURRENT_DATE
,
CURRENT_TIME
, and
CURRENT_TIMESTAMP
are synonyms for
CURDATE()
,
CURTIME()
, and
NOW()
, respectively.
The preceding functions return values in the client session time zone (see
Recipe 6.4
).
For values in UTC time, use the
UTC_DATE()
,
UTC_TIME()
, or
UTC_TIMESTAMP()
func‐
tions instead.
To determine the current date and time for an arbitrary time zone, pass the value of the
appropriate UTC function to
CONVERT_TZ()
(see
Recipe 6.5
).
To obtain subparts of these values, such as the current day of the month or current hour
of the day, use the decomposition techniques discussed in
Recipe 6.8
.
6.7. Using TIMESTAMP or DATETIME to Track Row-
Modification Times
Problem
You want to record row-creation time or last modification time automatically.
Solution
Use the auto-initialization and auto-update properties of the
TIMESTAMP
and
DATE
TIME
data types.
Discussion
MySQL supports
TIMESTAMP
and
DATETIME
data types that store date-and-time values.
Recipe 6.1
covers the range of values for these types. This section focuses on special