Home Quizzes Leaderboard Competitions Learn Hire Us
About Contact
Log In Sign Up
Learn PHP Working with Dates & Time

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');
Code Example
<?php

// Current date and time

$today = new DateTimeImmutable();

$now = new DateTimeImmutable();

echo "Date: " . $today->format('Y-m-d') . "\n";

echo "Time: " . $now->format('H:i:s') . "\n";

// Specific date

$birthday = new DateTimeImmutable('1990-06-15');

echo "Birthday: " . $birthday->format('l, F j, Y') . "\n";

// Formatting

echo $birthday->format('d/m/Y') . "\n"; // 15/06/1990

echo $birthday->format('M d, Y') . "\n"; // Jun 15, 1990

// Parsing from string

$parsed = new DateTimeImmutable('25/12/2024', new
DateTimeZone('UTC'));

// Or use createFromFormat for custom formats:

$parsed = DateTimeImmutable::createFromFormat('d/m/Y',
'25/12/2024');

echo $parsed->format('F d, Y') . "\n"; // December 25, 2024

// Date arithmetic --- always returns NEW object

$nextWeek = $today->add(new DateInterval('P7D'));

$lastMonth = $today->sub(new DateInterval('P1M'));

echo "Next week: " . $nextWeek->format('Y-m-d') . "\n";

echo "Last month: " . $lastMonth->format('Y-m-d') . "\n";

// Days and years old

$diff = $today->diff($birthday);

echo "Days old: {$diff->days}\n";

echo "Years old: {$diff->y}\n";

// Comparison

var_dump($today > $birthday); // bool(true)

// Event scheduler

$events = [

['name' => 'Team Meeting', 'dt' => new
DateTimeImmutable('2025-06-20 09:00')],

['name' => 'Code Review', 'dt' => new
DateTimeImmutable('2025-06-21 14:30')],

['name' => 'Release Party', 'dt' => new
DateTimeImmutable('2025-06-25 18:00')],

];

usort($events, fn($a, $b) => $a['dt'] <=> $b['dt']);

echo "\n=== Upcoming Events ===\n";

foreach ($events as $e) {

echo " {$e['name']} @ " . $e['dt']->format('D M d, Y
\a\t g:i A') . "\n";

}

?>
← Closures, Generators & Functional PHP Design Patterns →

Log in to track your progress and earn badges as you complete lessons.

Log In to Track Progress