Working with Dates and Time in PHP: Functions, Classes, and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Dates and Time in PHP: From Basics to Advanced Techniques



Working with dates and time is one of the most common tasks in web development. Whether it's displaying the current date, calculating the difference between events, or saving timestamps in a database, PHP provides a powerful and flexible toolkit. In this article, we will thoroughly examine the main functions, classes, and best practices for working with dates and time in PHP.



This material will be useful both for beginners just starting to learn PHP and for experienced developers looking to systematize their knowledge.



1. Basic Functions: date(), time() and mktime()



Let's start with the fundamentals. PHP offers several built-in functions for working with dates. The most popular ones are date(), time() and mktime().



The time() Function

The time() function returns the current Unix timestamp — the number of seconds that have passed since January 1, 1970 00:00:00 UTC. This is the base value from which many operations start.



echo time(); // Will output something like 1698765432


The date() Function

The date() function formats a local date and time. The first parameter is a format string, the second (optional) is a timestamp. If the second parameter is not specified, the current time is used.



// Basic formatsecho date("Y-m-d");          // 2023-10-31echo date("H:i:s");          // 14:30:00echo date("d.m.Y H:i");      // 31.10.2023 14:30echo date("l, j F Y");       // Tuesday, 31 October 2023

// Custom timestamp$timestamp = mktime(12, 0, 0, 1, 1, 2024);echo date("Y-m-d H:i:s", $timestamp); // 2024-01-01 12:00:00


Popular formatting characters:

  • d — day of the month (01-31)
  • m — month (01-12)
  • Y — year in 4-digit format
  • H — hours (00-23)
  • i — minutes (00-59)
  • s — seconds (00-59)


The mktime() Function

mktime() creates a timestamp based on the passed parameters (hours, minutes, seconds, month, day, year). This is convenient for getting the timestamp of a specific date.



// Create a timestamp for December 25, 2023, 10:30:00$christmas = mktime(10, 30, 0, 12, 25, 2023);echo date("d.m.Y H:i:s", $christmas); // 25.12.2023 10:30:00


2. Object-Oriented Approach: The DateTime Class



Although the date() and time() functions are simple, they have limitations. For more complex operations (comparing dates, working with time zones, adding/subtracting intervals), it is better to use the DateTime class.



Creating DateTime Objects

// Current time$now = new DateTime();echo $now->format("Y-m-d H:i:s"); // 2023-10-31 14:30:00

// Specific date$date = new DateTime("2024-03-15 08:00:00");echo $date->format("d.m.Y"); // 15.03.2024

// Relative formats$nextWeek = new DateTime("+1 week");echo $nextWeek->format("Y-m-d");

$lastMonth = new DateTime("first day of last month");echo $lastMonth->format("Y-m-d");


Working with Time Zones

One of the main advantages of DateTime is support for time zones.



// Specify time zone when creating$moscow = new DateTime("now", new DateTimeZone("Europe/Moscow"));echo $moscow->format("Y-m-d H:i:s"); // Moscow time

// Changing time zone$newYork = new DateTime("now", new DateTimeZone("America/New_York"));echo $newYork->format("Y-m-d H:i:s"); // New York time

// List of available zones: https://www.php.net/manual/en/timezones.php


Comparing Dates

$date1 = new DateTime("2024-01-01");$date2 = new DateTime("2024-12-31");

if ($date1 < $date2) { echo "The first date is earlier than the second";}

// Diff

Blogs

Book Recommendations