Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 7 of 8
Date & Time Functions
PHP has rich built-in date/time support. The DateTime and DateTimeImmutable classes (OOP approach) are preferred over the procedural date() function because they support timezones, comparisons, arithmetic, and immutable patterns. Always store dates in UTC in the database and convert to user timezone for display.
DateTime, DateInterval & Timezone
<?php
declare(strict_types=1);
// ── Procedural functions (simple use cases) ────────────────────
echo date("Y-m-d H:i:s"); // "2026-03-20 08:34:01" (server timezone)
echo date("U"); // Unix timestamp
echo time(); // current Unix timestamp (seconds since 1970)
echo mktime(12, 0, 0, 3, 20, 2026); // timestamp for specific date/time
// ── DateTime class (OOP - preferred) ──────────────────────────
$now = new DateTime("now", new DateTimeZone("UTC"));
echo $now->format("Y-m-d H:i:s"); // "2026-03-20 08:34:01"
echo $now->format("D, d M Y"); // "Fri, 20 Mar 2026"
echo $now->format("U"); // Unix timestamp
// From string
$publishDate = new DateTime("2026-01-15 09:00:00");
$tomorrow = new DateTime("+1 day");
$nextWeek = new DateTime("next Monday");
// ── DateTimeImmutable (preferred for safety) ──────────────────
// Creates a NEW object on modification - original unchanged
$today = new DateTimeImmutable("now", new DateTimeZone("UTC"));
$nextMonth = $today->modify("+1 month"); // $today unchanged!
$yesterday = $today->modify("-1 day");
// ── DateInterval - arithmetic ─────────────────────────────────
$start = new DateTimeImmutable("2026-01-01");
$end = new DateTimeImmutable("2026-03-20");
$diff = $start->diff($end); // DateInterval object
echo $diff->days; // 78 (total days)
echo $diff->m; // 2 (months component)
echo $diff->d; // 19 (days component)
// Add/subtract intervals
$dueDate = $today->add(new DateInterval("P30D")); // +30 days
$expired = $today->sub(new DateInterval("P1Y")); // -1 year
// ── Timezone handling ─────────────────────────────────────────
$utc = new DateTimeZone("UTC");
$ist = new DateTimeZone("Asia/Kolkata");
$utcTime = new DateTimeImmutable("2026-03-20 08:00:00", $utc);
$istTime = $utcTime->setTimezone($ist);
echo $istTime->format("Y-m-d H:i:s"); // "2026-03-20 13:30:00"
// ── Formatting for different contexts ─────────────────────────
$date = new DateTimeImmutable("2026-03-20");
echo $date->format("Y-m-d"); // "2026-03-20" - DB storage
echo $date->format("d/m/Y"); // "20/03/2026" - UK display
echo $date->format("M j, Y"); // "Mar 20, 2026" - US display
echo $date->format("D, d M Y H:i:s T"); // "Fri, 20 Mar 2026 08:00:00 UTC" - HTTP header
echo $date->format("c"); // ISO 8601: "2026-03-20T08:00:00+00:00" - APIQuick Quiz
Tip
Tip
Practice Date Time Functions in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Date & Time Functions
Practice Task
Note
Practice Task - (1) Write a working example of Date Time Functions from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Date Time Functions is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.