iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Date/Time Functions

Date / time reference. Procedural functions for quick work; OO classes (DateTimeImmutable, DateInterval, DateTimeZone) for anything serious.

Procedural

FunctionReturns
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 / gmmktimeBuild 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

CharMeans
Y / y4- / 2-digit year
m / nMonth (zero-padded / not)
d / jDay (zero-padded / not)
H / i / s24-hr hour, minute, second
D / lDay name (short / long)
M / FMonth name (short / long)
UUnix timestamp
cISO 8601
rRFC 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')

Test yourself

Q1. For "next monday" use…
Q2. Best storage is…
Q3. Immutable points-in-time class is…

Discussion

Loading…