Working with Dates & Time
⏱ 18 min read read
Date Functions Overview:
date('Y-m-d') // '2024-12-25' --- current date formatted
date('H:i:s') // '14:30:00' --- current time
time() // Unix timestamp (seconds since 1970)
mktime(h,m,s,month,day,year) // make timestamp
strtotime('next Monday') // parse human-readable date → timestamp
strtotime('+7 days') // relative date
DateTimeImmutable --- The Recommended Class:
Always use DateTimeImmutable over DateTime --- it never modifies the
original object.
// Create
$today = new DateTimeImmutable();
$birthday = new DateTimeImmutable('1990-06-15');
$specific = new DateTimeImmutable('2024-12-25 09:30:00');
// Format
$today->format('Y-m-d') // '2024-12-25'
$today->format('d/m/Y') // '25/12/2024'
$today->format('l, F j, Y') // 'Wednesday, December 25, 2024'
$today->format('H:i:s') // '14:30:00'
$today->format('g:i A') // '2:30 PM'
Date Arithmetic:
// DateInterval --- duration
$nextWeek = $today->add(new DateInterval('P7D')); // +7 days
$lastMonth = $today->sub(new DateInterval('P1M')); // -1 month
$nextYear = $today->modify('+1 year'); // shorthand
// Difference between dates
$diff = $today->diff($birthday);
echo $diff->days; // total days
echo $diff->y; // years
echo $diff->m; // months
Use DateTimeImmutable, NOT DateTime!
DateTime modifies itself in place --- easy source of bugs.
DateTimeImmutable returns NEW objects --- the original is unchanged.
$dt2 = $dt->add(...) --- must assign the result!
Timezones:
$tz = new DateTimeZone('Africa/Lagos');
$now = new DateTimeImmutable('now', $tz);
echo $now->format('Y-m-d H:i:s T');
date('Y-m-d') // '2024-12-25' --- current date formatted
date('H:i:s') // '14:30:00' --- current time
time() // Unix timestamp (seconds since 1970)
mktime(h,m,s,month,day,year) // make timestamp
strtotime('next Monday') // parse human-readable date → timestamp
strtotime('+7 days') // relative date
DateTimeImmutable --- The Recommended Class:
Always use DateTimeImmutable over DateTime --- it never modifies the
original object.
// Create
$today = new DateTimeImmutable();
$birthday = new DateTimeImmutable('1990-06-15');
$specific = new DateTimeImmutable('2024-12-25 09:30:00');
// Format
$today->format('Y-m-d') // '2024-12-25'
$today->format('d/m/Y') // '25/12/2024'
$today->format('l, F j, Y') // 'Wednesday, December 25, 2024'
$today->format('H:i:s') // '14:30:00'
$today->format('g:i A') // '2:30 PM'
Date Arithmetic:
// DateInterval --- duration
$nextWeek = $today->add(new DateInterval('P7D')); // +7 days
$lastMonth = $today->sub(new DateInterval('P1M')); // -1 month
$nextYear = $today->modify('+1 year'); // shorthand
// Difference between dates
$diff = $today->diff($birthday);
echo $diff->days; // total days
echo $diff->y; // years
echo $diff->m; // months
Use DateTimeImmutable, NOT DateTime!
DateTime modifies itself in place --- easy source of bugs.
DateTimeImmutable returns NEW objects --- the original is unchanged.
$dt2 = $dt->add(...) --- must assign the result!
Timezones:
$tz = new DateTimeZone('Africa/Lagos');
$now = new DateTimeImmutable('now', $tz);
echo $now->format('Y-m-d H:i:s T');
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress