How to Get Current Date and Time in PHP: Best Practices in 2025
\n\nEvery PHP developer, from beginner to senior, has asked themselves at least once: "How do I get the current date and time in PHP?". This query on Stack Overflow has garnered thousands of views and remains relevant. In 2025, as PHP continues to hold its ground in web development, the ability to work correctly with dates and times is not just a basic competency but a necessity. Logging, caching, reports, working with APIs — all of this requires precise time. In this article, we will cover not only classic functions but also modern best practices to make your code clean, reliable, and ready for any time zone.
\n\nThe Classics: date() and time()
\n\nThe simplest and fastest way to get the current date and time in PHP is the date() function. It takes a format string and returns a formatted date based on the server's current time or a specified timestamp.
<?php\necho date('Y-m-d H:i:s'); // 2025-04-10 14:30:00\necho date('d.m.Y'); // 10.04.2025\necho date('r'); // Thu, 10 Apr 2025 14:30:00 +0300\n?>\n\nWhat if you only need a Unix timestamp (the number of seconds since January 1, 1970)? Use time():
<?php\n$timestamp = time();\necho $timestamp; // 1744273800 (example)\necho date('Y-m-d H:i:s', $timestamp);\n?>\n\nImportant: date() uses the time zone set in php.ini via the date.timezone directive, or set by the date_default_timezone_set() function. In 2025, this is still a working option for simple scripts, but for serious projects, there are more flexible tools.
Modern Approach: The DateTime Class
\n\nStarting with PHP 5.2 (which was a long time ago), an object-oriented way of working with dates appeared in the language — the DateTime class. It has become the de facto standard for most frameworks and CMS. Why? Because it is more flexible, more readable, and safer.
<?php\n$now = new DateTime();\necho $now->format('Y-m-d H:i:s'); // 2025-04-10 14:30:00\n\n// With a specified time zone\n$now = new DateTime('now', new DateTimeZone('Europe/Moscow'));\necho $now->format('Y-m-d H:i:s'); // 2025-04-10 14:30:00 (MSK)\n?>\n\nDateTime allows you to easily manipulate dates: add days, subtract months, compare objects. For example:
<?php\n$today = new DateTime();\n$nextWeek = (new DateTime())->modify('+7 days');\n$diff = $nextWeek->diff($today);\necho $diff->days; // 7\n?>\n\nThis is the approach recommended in PHP best practices for 2025. It is immutable (if you don't modify the object directly) and intuitive.
\n\nImmutable Option: DateTimeImmutable
\n\nIf you are working on a serious project where predictability is important, use DateTimeImmutable. Unlike DateTime, its methods do not modify the original object but return a new one. This prevents accidental state mutations.
<?php\n$date = new DateTimeImmutable('2025-04-10');\n$newDate = $date->modify('+1 month');\necho $date->format('Y-m-d'); // 2025-04-10 (unchanged)\necho $newDate->format('Y-m-d'); // 2025-05-10\n?>\n\nIn 2025, this is considered good practice — use DateTimeImmutable wherever possible.
Time Zones: Configuration and Conversion
\n\nA time zone error is one of the most common problems. Imagine: the server is in the USA, the user is in Russia, and you are showing the time in Greenwich Mean Time. A classic failure.
\n\nTo avoid this, always explicitly specify the time zone. The most reliable way is to set it at the beginning of the script using date_default_timezone_set():