Code With Coffie
  • HOME
  • ABOUT US
  • PORTFOLIO
  • JAVASCRIPT
    • Vue.js
  • PHP
    • PHP OOPS
    • LARAVEL
    • WORDPRESS
  • MYSQL
    • DATETIME
  • DSA
    • LEETCODE
  • Home
  • Blog
  • MYSQL
  • DATETIME
  • MySQL Date and Time Basic Questions

MySQL Date and Time Basic Questions

Jun 13, 2025 by codewithhemu

๐Ÿ•’ 1. Get Current Date & Time

SELECT NOW();              -- Current date and time (e.g., 2025-06-13 20:15:00)
SELECT CURDATE();          -- Current date only
SELECT CURTIME();          -- Current time only
SELECT UTC_TIMESTAMP();    -- Current UTC date and time

๐Ÿ—“๏ธ 2. Date Formatting

-- Format a datetime field
SELECT DATE_FORMAT(NOW(), '%d-%m-%Y');       -- Output: 13-06-2025
SELECT DATE_FORMAT(NOW(), '%M %d, %Y');      -- Output: June 13, 2025
SELECT DATE_FORMAT(NOW(), '%b-%Y');          -- Output: Jun-2025

Common format codes:

%d - Day (01-31)
%m - Month (01-12)
%b / %M - Month name short/long
%Y - Year (4 digits)
%H:%i:%s - Time (Hours:Minutes:Seconds)

๐Ÿ“… 3. Extract Parts of a Date

SELECT YEAR(NOW());        -- 2025
SELECT MONTH(NOW());       -- 6
SELECT DAY(NOW());         -- 13
SELECT HOUR(NOW());        -- 20

๐Ÿงฎ 4. Date Calculations

-- Add/Subtract Days, Months, Years
SELECT DATE_ADD(NOW(), INTERVAL 7 DAY);         -- Add 7 days
SELECT DATE_SUB(NOW(), INTERVAL 1 MONTH);       -- Subtract 1 month
SELECT NOW() + INTERVAL 1 HOUR;                 -- Add 1 hour

๐Ÿ“† 5. Difference Between Dates

SELECT DATEDIFF('2025-06-20', '2025-06-13');      -- Days between: 7
SELECT TIMESTAMPDIFF(YEAR, '2000-01-01', NOW());  -- Age calculation
SELECT TIMEDIFF('12:30:00', '11:00:00');          -- Time difference: 01:30:00

๐Ÿงพ 6. Where Clause with Dates

SELECT * FROM orders WHERE order_date = CURDATE();       -- Today
SELECT * FROM orders WHERE order_date >= '2025-01-01';

-- Orders from last 30 days
SELECT * FROM orders 
WHERE order_date >= CURDATE() - INTERVAL 30 DAY;

โณ 7. Between Dates

SELECT * FROM orders
WHERE order_date BETWEEN '2025-06-01' AND '2025-06-10';

๐Ÿ•“ 8. Convert Timestamp / Unix Time

-- From Unix timestamp to readable date
SELECT FROM_UNIXTIME(1718293800);    -- Output: 2025-06-13 15:00:00

-- To Unix timestamp
SELECT UNIX_TIMESTAMP('2025-06-13 15:00:00');    -- Output: 1718293800

๐ŸŒ 9. Timezone Conversion

-- Convert UTC to IST (Asia/Kolkata)
SELECT CONVERT_TZ(NOW(), 'UTC', 'Asia/Kolkata');

๐Ÿงฐ 10. Miscellaneous

-- Get First/Last day of month
SELECT LAST_DAY(NOW());
SELECT DATE_FORMAT(NOW(), '%Y-%m-01');  -- First day of month

-- Week related
SELECT WEEK(NOW());        -- Week number
SELECT DAYOFWEEK(NOW());   -- 1 (Sunday) to 7 (Saturday)
  • Share:
Previous Article Master Array Sorting in PHP for DSA: Step-by-Step Guide with Examples
Next Article Dynamic period queries in MySQL for last week, last month, this year, etc.
No comments yet! You be the first to comment.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

category

  • DATETIME (6)
  • DJANGO (1)
  • Docker (1)
  • DSA (21)
  • DSA PRACTICE (4)
  • ENGLISH READING (1)
  • JAVASCRIPT (69)
  • LARAVEL (40)
  • LeetCode (1)
  • MYSQL (45)
  • PHP (21)
  • PHP OOPS (16)
  • PROGRAMME (1)
  • PYTHON (7)
  • REACT JS (6)
  • STAR PATTERN PROGRAMME (7)
  • Uncategorized (20)
  • Vue.js (5)
  • WORDPRESS (15)

Archives

  • March 2026
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • January 2023

Tags

Certificates Education Instructor Languages School Member

Building reliable software solutions for modern businesses. Sharing practical tutorials and real-world project insights to help developers grow with confidence.

GET HELP

  • Home
  • Portfolio
  • Privacy Policy
  • Terms & Conditions
  • Disclaimer
  • Contact Us

PROGRAMS

  • Software Development
  • Performance Optimization
  • System Architecture
  • Project Consultation
  • Technical Mentorship

CONTACT US

  • Netaji Subhash Place (NSP) Delhi
  • Tel: + (91) 8287315524
  • Email: contact@codewithcoffie.com

Copyright ยฉ 2026 LearnPress LMS | Powered by LearnPress LMS