Date/Time Functions
Date / time reference. Procedural functions for quick work; OO classes (DateTimeImmutable, DateInterval, DateTimeZone) for anything serious.
Procedural
| Function | Returns |
|---|---|
time() | Current Unix timestamp. |
microtime(true) | High-precision timestamp (float). |
date($fmt, $ts = null) | Format a timestamp. |
gmdate($fmt) | Same but UTC. |
strtotime('next monday') | Parse English-ish dates. |
mktime / gmmktime | Build a timestamp from parts. |
checkdate($m, $d, $y) | True if the date is valid. |
date_default_timezone_set('UTC') | Set the script's zone. |
OO
PHP
$now = new DateTimeImmutable('now');
$soon = $now->add(new DateInterval('PT15M')); // +15 min
$diff = $now->diff(new DateTimeImmutable('2027-01-01'));
$inAUS = $now->setTimezone(new DateTimeZone('Australia/Sydney'));
Common format characters
| Char | Means |
|---|---|
Y / y | 4- / 2-digit year |
m / n | Month (zero-padded / not) |
d / j | Day (zero-padded / not) |
H / i / s | 24-hr hour, minute, second |
D / l | Day name (short / long) |
M / F | Month name (short / long) |
U | Unix timestamp |
c | ISO 8601 |
r | RFC 2822 |
Intervals
DateInterval uses ISO 8601 duration syntax: P1Y2M3DT4H5M6S = 1 year, 2 months, 3 days, 4 hours, 5 minutes, 6 seconds. P is the prefix, T separates date from time.
Tip: Always set the timezone explicitly in PHP — either in
php.ini (date.timezone = "UTC") or with date_default_timezone_set('UTC'). Default is "UTC", but never assume.Example
Example
<?php
echo date('Y-m-d'), PHP_EOL;
echo time(), PHP_EOL;
echo strtotime('next monday'), PHP_EOL;
$d = new DateTimeImmutable('2026-06-06');
echo $d->format('l');
Try it Yourself »
Exercise
Parse "next monday" as a timestamp.
('next monday')
Nine letters.
Discussion
Loading…