Working with Data Lesson 8 of 13
MySQL Date and Time Functions
Work with dates in MySQL — NOW, CURDATE, DATE_FORMAT, YEAR, MONTH, DAYNAME, DATEDIFF, DATE_ADD, TIMESTAMPDIFF for ages, and grouping sales by day and month.
Dates are everywhere in real systems: birthdays, enrollment dates, due dates, sales records. MySQL has rich date functions for extracting parts, formatting, and doing date math.
Current date and time
SELECT NOW(), CURDATE(), CURTIME();
Output
+---------------------+------------+-----------+ | NOW() | CURDATE() | CURTIME() | +---------------------+------------+-----------+ | 2026-12-02 14:35:08 | 2026-12-02 | 14:35:08 | +---------------------+------------+-----------+
These use the MySQL server's time zone. For Philippine time, the server (or the session, with SET time_zone = '+08:00';) should be set to UTC+8.
Extracting parts
SELECT order_date,
YEAR(order_date) AS y,
MONTH(order_date) AS m,
DAY(order_date) AS d,
DAYNAME(order_date) AS weekday,
MONTHNAME(order_date) AS month_name
FROM orders
LIMIT 3;
Output
+------------+------+------+------+----------+------------+ | order_date | y | m | d | weekday | month_name | +------------+------+------+------+----------+------------+ | 2026-11-02 | 2026 | 11 | 2 | Monday | November | | 2026-11-02 | 2026 | 11 | 2 | Monday | November | | 2026-11-05 | 2026 | 11 | 5 | Thursday | November | +------------+------+------+------+----------+------------+
Formatting dates
SELECT DATE_FORMAT('2026-12-25', '%M %e, %Y') AS long_date,
DATE_FORMAT('2026-12-25', '%m/%d/%Y') AS us_style,
DATE_FORMAT('2026-12-25 19:30:00', '%W, %h:%i %p') AS with_time;
Output
+-------------------+------------+---------------------+ | long_date | us_style | with_time | +-------------------+------------+---------------------+ | December 25, 2026 | 12/25/2026 | Friday, 07:30 PM | +-------------------+------------+---------------------+
| Code | Meaning | Example |
|---|---|---|
%Y | 4-digit year | 2026 |
%m / %c | Month number (with / without leading zero) | 03 / 3 |
%M / %b | Month name / short name | March / Mar |
%d / %e | Day (with / without leading zero) | 05 / 5 |
%W / %a | Weekday / short weekday | Monday / Mon |
%h:%i %p | 12-hour time | 07:30 PM |
%H:%i | 24-hour time | 19:30 |
To read dates typed in other formats, use STR_TO_DATE('12/25/2026', '%m/%d/%Y').
Date math
SELECT DATEDIFF('2026-12-25', '2026-12-02') AS days_to_christmas,
DATE_ADD('2026-12-02', INTERVAL 7 DAY) AS due_date,
DATE_SUB('2026-12-02', INTERVAL 1 MONTH) AS last_month,
LAST_DAY('2026-02-10') AS end_of_feb;
Output
+-------------------+------------+------------+------------+ | days_to_christmas | due_date | last_month | end_of_feb | +-------------------+------------+------------+------------+ | 23 | 2026-12-09 | 2026-11-02 | 2026-02-28 | +-------------------+------------+------------+------------+
Computing age
SELECT first_name, birthday,
TIMESTAMPDIFF(YEAR, birthday, '2026-12-02') AS age
FROM students;
Output
+------------+------------+------+ | first_name | birthday | age | +------------+------------+------+ | Juan | 2011-03-14 | 15 | | Maria | 2011-07-02 | 15 | | Jose | 2010-12-19 | 15 | | Andres | 2012-01-30 | 14 | | ... | ... | ... | +------------+------------+------+
TIMESTAMPDIFF(YEAR, ...) counts complete years, so it correctly handles birthdays that haven't happened yet this year. Use CURDATE() instead of the fixed date to compute today's age.
Grouping by date
Live example (SQLite uses strftime where MySQL uses DATE_FORMAT):
SELECT strftime('%Y-%m', order_date) AS month,
COUNT(*) AS orders
FROM orders
GROUP BY month;
SELECT order_date, COUNT(*) AS orders
FROM orders
GROUP BY order_date
ORDER BY orders DESC
LIMIT 3;
In MySQL, monthly totals are usually written GROUP BY DATE_FORMAT(order_date, '%Y-%m') or GROUP BY YEAR(order_date), MONTH(order_date).
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04