PHP Date & Time
Dates appear everywhere in web applications: order timestamps, "posted 3 days ago", subscription renewals, opening hours. PHP has two toolsets for them: the classic functions (date(), mktime(), strtotime()) built around Unix timestamps, and the DateTime classes, which are more powerful and easier to get right.
Note: To keep every example's output identical whenever you run it, this lesson always sets the time zone to UTC and uses fixed dates. In a real application you would often call
time()ornew DateTimeImmutable()(meaning "now").
Time zones first
PHP needs to know which time zone to use when it turns a moment into a calendar date. Set it once at the start of your script (or in php.ini with date.timezone):
date_default_timezone_set('UTC');Unix timestamps and date()
A Unix timestamp is the number of seconds since 1 January 1970 00:00:00 UTC. date($format, $timestamp) turns one into readable text using format letters:
| Letter | Meaning | Example |
|---|---|---|
Y / y |
4- / 2-digit year | 2025 / 25 |
m / n |
month with / without leading zero | 03 / 3 |
d / j |
day with / without leading zero | 07 / 7 |
D / l |
short / full weekday name | Fri / Friday |
M / F |
short / full month name | Mar / March |
H / G |
24-hour hour with / without zero | 09 / 9 |
h / A |
12-hour hour / AM or PM | 09 / AM |
i / s |
minutes / seconds | 30 / 05 |
N |
ISO weekday (1 = Monday) | 5 |
t |
days in the month | 31 |
L |
leap year? (1/0) | 0 |
<?php
date_default_timezone_set('UTC');
$ts = 1741944600; // a fixed moment: 14 March 2025, 09:30 UTC
echo date('Y-m-d H:i:s', $ts) . PHP_EOL;
echo date('l, j F Y', $ts) . PHP_EOL;
echo date('D d/m/y \a\t h:i A', $ts) . PHP_EOL;
echo "Days in this month: " . date('t', $ts) . PHP_EOL;
echo "Leap year? " . (date('L', $ts) ? 'yes' : 'no') . PHP_EOL;2025-03-14 09:30:00
Friday, 14 March 2025
Fri 14/03/25 at 09:30 AM
Days in this month: 31
Leap year? no
Escape a letter with a backslash (\a\t) to print it literally.
Making timestamps: mktime() and strtotime()
mktime(hour, minute, second, month, day, year) builds a timestamp from parts. strtotime() understands English descriptions — absolute dates and also relative phrases, calculated from a base timestamp you can pass as the second argument:
<?php
date_default_timezone_set('UTC');
$launch = mktime(18, 0, 0, 12, 31, 2025);
echo "Launch: " . date('Y-m-d H:i', $launch) . PHP_EOL;
$base = strtotime('2025-03-14 09:30:00');
echo "Base: " . date('D Y-m-d', $base) . PHP_EOL;
echo "+1 week: " . date('D Y-m-d', strtotime('+1 week', $base)) . PHP_EOL;
echo "next Monday: " . date('D Y-m-d', strtotime('next monday', $base)) . PHP_EOL;
echo "last day of month: " . date('Y-m-d', strtotime('last day of this month', $base)) . PHP_EOL;
// mktime() normalises overflowing values: month 14 = February of next year
echo date('Y-m-d', mktime(0, 0, 0, 14, 1, 2025)) . PHP_EOL;Launch: 2025-12-31 18:00
Base: Fri 2025-03-14
+1 week: Fri 2025-03-21
next Monday: Mon 2025-03-17
last day of month: 2025-03-31
2026-02-01
DateTimeImmutable: the modern choice
The DateTimeImmutable class wraps a moment together with its time zone. Methods like modify() and add() return a new object and leave the original untouched, which avoids a whole class of bugs.
<?php
date_default_timezone_set('UTC');
$order = new DateTimeImmutable('2025-03-14 09:30:00');
$delivery = $order->add(new DateInterval('P3DT4H')); // 3 days, 4 hours
$reminder = $order->modify('+1 month');
echo "Ordered: " . $order->format('Y-m-d H:i') . PHP_EOL;
echo "Delivery: " . $delivery->format('Y-m-d H:i') . PHP_EOL;
echo "Reminder: " . $reminder->format('Y-m-d') . PHP_EOL;
// Different time zones for the same moment
$hanoi = $order->setTimezone(new DateTimeZone('Asia/Ho_Chi_Minh'));
$ny = $order->setTimezone(new DateTimeZone('America/New_York'));
echo "In Hanoi: " . $hanoi->format('Y-m-d H:i T P') . PHP_EOL;
echo "In New York: " . $ny->format('Y-m-d H:i T P') . PHP_EOL;Ordered: 2025-03-14 09:30
Delivery: 2025-03-17 13:30
Reminder: 2025-04-14
In Hanoi: 2025-03-14 16:30 +07 +07:00
In New York: 2025-03-14 05:30 EDT -04:00
DateInterval specs start with P (period); Y, M, D are years, months, days, and after a T come H, M, S for hours, minutes, seconds.
Differences between dates
diff() returns a DateInterval describing the gap between two dates. Its days property gives the total number of days:
<?php
date_default_timezone_set('UTC');
$joined = new DateTimeImmutable('2023-11-02');
$today = new DateTimeImmutable('2025-03-14');
$gap = $joined->diff($today);
echo "Member for {$gap->y} year(s), {$gap->m} month(s), {$gap->d} day(s)" . PHP_EOL;
echo "That is {$gap->days} days in total" . PHP_EOL;
echo $gap->format('%a days, invert=%R') . PHP_EOL;
// Parse a date in a known format
$d = DateTimeImmutable::createFromFormat('d/m/Y H:i', '25/12/2025 08:15');
echo $d->format('l jS \of F Y') . PHP_EOL;
// Comparison operators work on DateTime objects
var_dump($joined < $today);Member for 1 year(s), 4 month(s), 12 day(s)
That is 498 days in total
498 days, invert=+
Thursday 25th of December 2025
bool(true)
Tip: Store dates in databases as UTC (e.g.
2025-03-14 09:30:00or a timestamp) and convert to the user's time zone only when displaying them.
Test yourself
Which date() format prints 2025-03-14?
d-m-YY-m-dy-M-DY-n-j
What does $b = $a->modify('+1 day'); do when $a is a DateTimeImmutable?
- Changes
$aand returns it - Returns a new object;
$ais unchanged - Throws an exception
- Returns
true
What does the DateInterval spec P1MT30M mean?
- 1 minute and 30 months
- 1 month and 30 minutes
- 1 month and 30 seconds
- Invalid spec