Dates & Times (DateTime)
Store, format and compare dates the right way using the DateTime class — timezones, formatting, and the gap between two dates.
What you will learn
- Create and format dates with DateTime
- Set and respect timezones
- Measure the difference between two dates
Why a DateTime class?
Almost every real app deals with dates — when a post was published, how long until an event, a user signup time. PHP has an old date() function, but the modern, reliable way is the DateTime class. It handles timezones, comparisons and the messy edge cases (leap years, month lengths) for you, so you never do date maths by hand.
A *timezone* is the local clock for a place (Mumbai, London, New York). The same instant shows a different wall-clock time in each, so storing the timezone matters.
Creating and formatting a date
You make a date with new DateTime(...) and turn it into readable text with ->format(...), where letters stand for parts of the date (Y = 4-digit year, m = month, d = day, H:i = hours:minutes).
<?php
$now = new DateTime("now", new DateTimeZone("Asia/Kolkata"));
echo $now->format("Y-m-d"); // e.g. 2026-06-13
echo "<br>";
echo $now->format("l, d M Y H:i"); // e.g. Saturday, 13 Jun 2026 09:30
?>new DateTime("now", new DateTimeZone("Asia/Kolkata")) creates an object for the current moment, anchored to India Standard Time. ->format("Y-m-d") then renders it as year-month-day. The second format string mixes more letters: l is the full weekday name, M the short month, H:i the 24-hour time — so you get a friendly, human sentence.
Note: Output (in the browser, on 13 June 2026 at 9:30 am IST): 2026-06-13 Saturday, 13 Jun 2026 09:30 The same DateTime object produced two different displays just by changing the format string. The actual instant never changed — only how we wrote it out.
Tip: Use DateTimeImmutable instead of DateTime when you do not want a date to change accidentally — methods return a *new* date rather than altering the original. It is the safer default in modern code.
The gap between two dates
A common task is "how long between these two dates?". ->diff(...) compares two DateTime objects and returns a DateInterval describing the gap in years, months, days, etc.
<?php
$start = new DateTime("2026-01-01");
$today = new DateTime("2026-06-13");
$gap = $start->diff($today);
echo "It has been " . $gap->days . " days.";
echo "<br>That is " . $gap->m . " months and " . $gap->d . " days.";
?>$start->diff($today) works out the distance between 1 January and 13 June 2026 and hands back a DateInterval object stored in $gap. From it, $gap->days is the total number of days, while $gap->m and $gap->d break the same gap down into whole months plus leftover days.
Note: Output (in the browser): It has been 163 days. That is 5 months and 12 days. PHP did all the calendar arithmetic — different month lengths, no leap-year trip-ups. This is exactly the kind of "X days ago" or "ends in Y days" logic real sites show.
Q. Why prefer the DateTime class over manual date arithmetic?
✍️ Practice
- Print today’s date formatted as
d/m/Yin your own timezone. - Work out how many days until a future date (e.g. a birthday) using
diff.
🏠 Homework
- Write a function
timeAgo($date)that returns "X days ago" for any past date using DateTime diff.