📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners Date and Time

Date and Time

5 min read Quiz at the end
PHP makes it easy to work with dates and times using the date() function and DateTime class. You can format dates, calculate differences, and handle timezones. Always store dates in UTC and convert to local time only for display.

Date and Time

// Current timestamp
time();                        // Unix timestamp (seconds)
date("Y-m-d H:i:s");          // 2024-01-15 10:30:00
date("d/m/Y");                 // 15/01/2024
date("l, F j, Y");             // Monday, January 15, 2024
date("D, d M Y H:i:s");       // Mon, 15 Jan 2024 10:30:00

// Modify dates
strtotime("+7 days");          // timestamp 7 days from now
strtotime("next Monday");
strtotime("2024-12-25");       // parse date string

// DateTime class (recommended)
$dt = new DateTime();
$dt->modify("+1 month");
echo $dt->format("Y-m-d");

// Date difference
$d1 = new DateTime("2024-01-01");
$d2 = new DateTime("2024-12-31");
$diff = $d1->diff($d2);
echo $diff->days;  // 365
Topic Quiz · 5 questions

Test your understanding before moving on

1. What does time() return?
💡 time() returns the current Unix timestamp — seconds since the Unix epoch.
2. Which function formats a timestamp?
💡 date("Y-m-d", $timestamp) formats a Unix timestamp as a date string.
3. What does strtotime("+1 week") return?
💡 strtotime() converts a human-readable date string to a Unix timestamp.
4. Which PHP class handles dates in an OOP way?
💡 The DateTime class provides OOP interface for date/time manipulation.
5. How do you find the difference between two dates?
💡 DateTime::diff() returns a DateInterval object with the difference.