← Back to Chapters

MySQL Date Functions

? MySQL Date Functions

? Quick Overview

MySQL provides powerful date and time functions that help developers manipulate, extract, compare, and format date values efficiently in databases.

? Key Concepts

  • Date extraction and formatting
  • Adding and subtracting time intervals
  • Comparing dates and calculating differences

? Syntax & Theory

Date functions operate on DATE, DATETIME, or TIMESTAMP values and return formatted or calculated results.

? Code Examples

Get Current Date

? View Code Example
// Fetches today's date
SELECT CURDATE() AS current_date;

Get Current Date & Time

? View Code Example
// Returns current date and time
SELECT NOW() AS current_datetime;

Extract Date

? View Code Example
// Extracts date from datetime
SELECT DATE('2025-07-18 14:23:45') AS extracted_date;

Add Days

? View Code Example
// Adds 7 days to given date
SELECT DATE_ADD('2025-07-18', INTERVAL 7 DAY) AS new_date;

Subtract Months

? View Code Example
// Subtracts 3 months from date
SELECT DATE_SUB('2025-07-18', INTERVAL 3 MONTH) AS new_date;

Format Date

? View Code Example
// Formats date into readable string
SELECT DATE_FORMAT('2025-07-18', '%W, %M %d, %Y') AS formatted_date;

? Live Output / Explanation

These queries return calculated or formatted dates directly from the MySQL server, useful for reports and dashboards.

? Interactive Concept

Combine CURDATE() with DATE_ADD() to dynamically calculate deadlines or expiration dates.

? Use Cases

  • Generating reports with date filters
  • Calculating subscription expiry
  • Tracking user activity timelines

✅ Tips & Best Practices

  • Use DATE_FORMAT() for user-friendly displays
  • Ensure consistent date formats
  • Prefer server-side date calculations

? Try It Yourself

  • Calculate days remaining in the month
  • Format today’s date in different styles
  • Add 1 year to the current date