Date and time functions are among the most frequently used in MySQL. They help you extract parts of dates, calculate differences, format timestamps for display, and perform date arithmetic.
Essential Date Functions
- NOW(): Current date and time
- CURDATE(): Current date only
- YEAR(d), MONTH(d), DAY(d): Extract date parts
- DATE_FORMAT(d, fmt): Format a date as a string
- DATEDIFF(d1, d2): Number of days between two dates
- DATE_ADD(d, INTERVAL n unit): Add an interval to a date
- TIMESTAMPDIFF(unit, d1, d2): Difference in specified units
Formatting Dates
DATE_FORMAT uses format specifiers like %Y (4-digit year), %m (2-digit month), %d (2-digit day), %H:%i:%s (time). This is essential for generating reports with human-readable dates without post-processing in code.