Working with Dates and Time in PHP: From A to Z
Working with dates and time is one of the most common tasks in web development. Whether it's displaying a publication date, calculating a user's age, logging events, or creating a calendar — PHP provides a powerful and flexible toolkit. In this article, we will cover all aspects: from simple functions to an object-oriented approach with the DateTime class, including time zones and practical examples.
Basic Functions: date() and time()
The basic set of functions for working with dates includes time() and date(). The time() function returns the current Unix timestamp (the number of seconds since January 1, 1970). The date() function formats this timestamp into a human-readable form.
Getting the Current Date and Time
The simplest way to output the current date:
<?phpecho date('Y-m-d H:i:s'); // 2025-04-10 15:30:45?>Formatting with date()
The date() function takes two parameters: the format and an optional timestamp. The most popular formatting characters:
d— day of the month (01–31)m— month (01–12)Y— year in 4-digit formatH— hours in 24-hour format (00–23)i— minutes (00–59)s— seconds (00–59)
Example output with text:
<?phpecho 'Today is ' . date('d.m.Y') . ', time ' . date('H:i');// Today is 10.04.2025, time 15:30?>The DateTime Class: An Object-Oriented Approach
For complex operations, it is recommended to use the DateTime class. It is intuitive, supports time zones, and simplifies date manipulations.
Creating a DateTime Object
You can create an object in several ways:
<?php// Current date and time$now = new DateTime();echo $now->format('Y-m-d H:i:s'); // 2025-04-10 15:30:45
// Specific date$date = new DateTime('2025-12-31 23:59:59');echo $date->format('d.m.Y'); // 31.12.2025
// Using a relative format$nextWeek = new DateTime('+1 week');echo $nextWeek->format('Y-m-d');?>Modifying a Date: modify()
The modify() method allows you to easily add or subtract intervals:
<?php$date = new DateTime('2025-04-10');$date->modify('+10 days');echo $date->format('d.m.Y'); // 20.04.2025
$date->modify('-1 month');echo $date->format('d.m.Y'); // 20.03.2025?>Working with Time Zones
Ignoring time zones is a common mistake. PHP offers two ways to manage them: globally via date_default_timezone_set() and locally via the DateTimeZone object.
Global Setting
Set the time zone at the beginning of the script:
<?phpdate_default_timezone_set('Europe/Moscow');echo date('H:i'); // Moscow time?>Local Setting for DateTime
Each DateTime object can have its own time zone:
<?php$timezone = new DateTimeZone('Asia/Vladivostok');$date = new DateTime('now', $timezone);echo $date->format('H:i'); // Vladivostok time
// Changing the time zone of an existing object$date->setTimezone(new DateTimeZone('Europe/Moscow'));echo $date->format('H:i'); // Converted time?>Difference Between Dates: DateInterval
The DateInterval class and the diff() method allow you to calculate the difference between two dates.
Calculating the Difference
<?php$start = new DateTime('2025-01-01');$end = new DateTime('2025-12-31');$interval = $start->diff($end);
echo $interval->days; // 364 (number