Date and Time in PHP in 2025: Don't Repeat These 5 Mistakes

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

Time Waits for No One: Why Dates Are Complicated in PHP (and How to Fix It)



Imagine: you are writing a blog, an online store, or a personal account. And suddenly you realize — you need to show the user when an article was published, an order was made, or a message was sent. It seems simple, right? Just use the date() function — and you're done.



But the devil, as always, is in the details. In 2025, PHP developers are still making the same mistakes: confusing time zones, forgetting about object immutability, using outdated functions. Then they wonder why the time on the site "jumps" and tests fail.



In this article, we will cover best practices for working with date and time in PHP — from simple solutions to professional techniques. You will learn how to get the current date, how to work with time zones, and how to avoid shooting yourself in the foot when working with timestamps.



1. The Simplest Way: The date() Function



Let's start with the classic. The date() function is where 90% of beginners start. It takes two parameters: the output format and (optionally) a timestamp. If no timestamp is specified, the server's current time is used.



echo date('Y-m-d H:i:s');// Output: 2025-04-08 14:35:12


Here are the most popular formats:

  • Y — year (4 digits)
  • m — month (01–12)
  • d — day of the month (01–31)
  • H — hours in 24-hour format
  • i — minutes
  • s — seconds


The Problem: date() depends on the date.timezone setting in php.ini. If you haven't set a time zone, PHP will use the server's system time. And the server might be in a different time zone — then your users will see the wrong time.



2. The Professional Approach: The DateTime Class



Since PHP 5.2, we have the DateTime class. This is an object-oriented way to work with dates, which is much more flexible and safer than the date() and time() functions.



$now = new DateTime();echo $now->format('Y-m-d H:i:s');// Output: 2025-04-08 14:35:12


Advantages of DateTime:

  • You can easily set the time zone via the constructor
  • Supports date modification (add days, months)
  • Can compare dates with each other
  • Works with intervals


Example with a time zone:



$now = new DateTime('now', new DateTimeZone('Europe/Moscow'));echo $now->format('Y-m-d H:i:s');// Output: 2025-04-08 17:35:12 (MSK, UTC+3)


2.1. DateTimeImmutable — Your New Best Friend



In PHP 5.5, DateTimeImmutable appeared. Its main difference from regular DateTime: it does not modify itself. Instead, each method (e.g., modify() or add()) returns a new object.



$date = new DateTimeImmutable();$modified = $date->modify('+1 day');

echo $date->format('Y-m-d'); // 2025-04-08 (unchanged!)echo $modified->format('Y-m-d'); // 2025-04-09 (new object)


Why is this important? Because regular DateTime is a reference object. If you pass it to a function and it gets modified there, the original also changes. With Immutable, there is no such problem. In 2025, this is considered the de facto standard for serious projects.



3. How to Get a Timestamp: time() vs DateTime



Sometimes you need not a date, but a Unix timestamp — the number of seconds since January 1, 1970. PHP offers two ways:



// Old wayecho time();  // 1744112112

// New wayecho (new DateTime())->getTimestamp(); // 1744112112


For simple tasks, time() is perfectly

Blogs

Book Recommendations