Lessons → PHP Date & Time

PHP Date & Time

PHP
⏱ 18 min read📖 BeginnerNot completed

Working with dates and times is essential in almost every PHP application — from showing "posted 2 hours ago" to scheduling events and filtering records by date.

Basic date() Function

PHP
<?php
// Current date and time
echo date('Y-m-d');           // 2025-05-22
echo date('d/m/Y');           // 22/05/2025
echo date('H:i:s');           // 14:30:45
echo date('Y-m-d H:i:s');     // 2025-05-22 14:30:45
echo date('D, d M Y');        // Thu, 22 May 2025
echo date('l');               // Thursday
echo date('N');               // 4 (day of week, 1=Mon, 7=Sun)
echo date('U');               // Unix timestamp
?>

Common Format Characters

CharacterOutputExample
Y4-digit year2025
y2-digit year25
mMonth with leading zero05
MMonth abbreviationMay
FFull month nameMay
dDay with leading zero08
DDay abbreviationThu
lFull day nameThursday
H24-hour format14
h12-hour format02
iMinutes30
sSeconds45
AAM/PMPM

DateTime Class (Modern Way)

PHP
<?php
// Create DateTime objects
$now   = new DateTime();
$date  = new DateTime('2025-12-25');
$date2 = new DateTime('next Monday');
$date3 = new DateTime('+1 week');

// Format
echo $now->format('Y-m-d H:i:s');   // 2025-05-22 14:30:45
echo $date->format('D, d M Y');      // Thu, 25 Dec 2025

// Modify
$now->modify('+1 day');
$now->modify('-2 weeks');
$now->modify('next Friday');

// Compare dates
$date1 = new DateTime('2025-01-01');
$date2 = new DateTime('2025-12-31');

if ($date1 < $date2) {
    echo "date1 is earlier";
}

// Difference between dates
$diff = $date1->diff($date2);
echo $diff->days . " days";    // 364 days
echo $diff->months . " months"; // 11 months
?>

Timestamps

PHP
<?php
// Current Unix timestamp (seconds since Jan 1 1970)
$timestamp = time();       // e.g. 1716374445
$timestamp = mktime();     // same thing

// Create timestamp from specific date
$ts = mktime(14, 30, 0, 12, 25, 2025); // 2:30pm Dec 25 2025

// Format a timestamp
echo date('Y-m-d', $timestamp);
echo date('Y-m-d', strtotime('2025-12-25'));

// strtotime — convert string to timestamp
$ts = strtotime('next Monday');
$ts = strtotime('+2 weeks');
$ts = strtotime('2025-01-01');
$ts = strtotime('+30 days', strtotime('2025-01-01')); // Jan 31

echo date('Y-m-d', $ts);
?>

Timezones

PHP
<?php
// Set default timezone
date_default_timezone_set('Asia/Tokyo');

echo date('Y-m-d H:i:s');  // Tokyo time

// DateTime with timezone
$tokyo  = new DateTime('now', new DateTimeZone('Asia/Tokyo'));
$london = new DateTime('now', new DateTimeZone('Europe/London'));
$utc    = new DateTime('now', new DateTimeZone('UTC'));

echo $tokyo->format('H:i');   // 14:30
echo $london->format('H:i');  // 06:30 (UTC+9 vs UTC+1)

// Convert timezone
$dt = new DateTime('2025-05-22 14:30:00', new DateTimeZone('UTC'));
$dt->setTimezone(new DateTimeZone('Asia/Tokyo'));
echo $dt->format('Y-m-d H:i:s');  // 2025-05-22 23:30:00
?>

Date Calculations — Real Examples

PHP
<?php
// "Posted X ago" — like social media
function timeAgo(string $datetime): string {
    $now  = new DateTime();
    $past = new DateTime($datetime);
    $diff = $now->diff($past);

    if ($diff->y > 0)  return $diff->y . ' year'   . ($diff->y > 1 ? 's' : '') . ' ago';
    if ($diff->m > 0)  return $diff->m . ' month'  . ($diff->m > 1 ? 's' : '') . ' ago';
    if ($diff->d > 0)  return $diff->d . ' day'    . ($diff->d > 1 ? 's' : '') . ' ago';
    if ($diff->h > 0)  return $diff->h . ' hour'   . ($diff->h > 1 ? 's' : '') . ' ago';
    if ($diff->i > 0)  return $diff->i . ' minute' . ($diff->i > 1 ? 's' : '') . ' ago';
    return 'just now';
}

echo timeAgo('2025-05-20 10:00:00');  // 2 days ago
echo timeAgo('2025-05-22 13:00:00');  // 1 hour ago

// Check if date is in the past
function isPast(string $date): bool {
    return new DateTime($date) < new DateTime();
}

// Get all days in a month
$days = cal_days_in_month(CAL_GREGORIAN, 2, 2024);
echo $days;  // 29 (leap year!)

// Check if leap year
echo date('L');  // 1 if current year is leap year
?>
💡
In Laravel use Carbon! Carbon is a PHP DateTime extension that makes date manipulation beautiful: Carbon::now()->addDays(7)->format('Y-m-d'), Carbon::parse($date)->diffForHumans() (gives "2 hours ago" automatically).
← Previous Lesson Next Lesson →
🧠

Test your knowledge!

Take a quiz to reinforce what you learned.

Take Quiz →