Skip to Content
MySQL Cookbook, 2nd Edition
book

MySQL Cookbook, 2nd Edition

by Paul DuBois
November 2006
Intermediate to advanced
977 pages
30h 42m
English
O'Reilly Media, Inc.
Content preview from MySQL Cookbook, 2nd Edition

Determining the Current Date or Time

Problem

What’s today’s date? What time is it?

Solution

Use the CURDATE() , CURTIME() or NOW() functions to get values expressed with respect to the connection time zone. Use UTC_DATE(), UTC_TIME(), or UTC_TIMESTAMP() for values in UTC time.

Discussion

Some applications need to know the current date or time, such as those that write log records tagged with the current date and time. This kind of information is also useful for date calculations that are performed 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 current date and time are available through three functions. CURDATE() and CURTIME() return the date and time separately, and NOW() returns them both as a date-and-time value:

mysql>SELECT CURDATE(), CURTIME(), NOW();
+------------+-----------+---------------------+
| CURDATE()  | CURTIME() | NOW()               |
+------------+-----------+---------------------+
| 2006-06-03 | 09:41:50  | 2006-06-03 09:41:50 |
+------------+-----------+---------------------+

CURRENT_DATE and CURRENT_TIME are synonyms for CURDATE() and CURTIME(). CURRENT_TIMESTAMP is a synonym for NOW().

The preceding functions return values for the client’s connection time zone (Setting the Client Time Zone). For values in UTC time, use the UTC_DATE(), UTC_TIME(), or UTC_TIMESTAMP() functions instead.

To obtain subparts of these values (such as the current day of the month or current hour of the day), ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

MySQL Cookbook, 3rd Edition

MySQL Cookbook, 3rd Edition

Paul DuBois
MySQL 8 Cookbook

MySQL 8 Cookbook

Karthik Appigatla
MySQL Cookbook

MySQL Cookbook

Paul DuBois
MySQL Cookbook, 4th Edition

MySQL Cookbook, 4th Edition

Sveta Smirnova, Alkin Tezuysal

Publisher Resources

ISBN: 059652708XSupplemental ContentErrata Page