Introduction: Why is the question "How to get the current year in PHP?" so popular?
It would seem, what could be simpler? Just take and output the current year. But on Stack Overflow, this question has garnered hundreds of thousands of views. Why? Because behind the apparent simplicity lie dozens of nuances: from differences between server time and the user's timezone to proper formatting for different locales.
In this article, we will break down how to get the current year in PHP using all possible methods, learn to avoid typical mistakes, and understand which method to choose for your specific task. Whether you are writing a copyright notice in a website footer or a complex time tracking system — this material will be useful.
Let's go!
Method #1: The Classic date() Function
The most popular and simple way to get the current year in PHP is to use the date() function. It returns a formatted string with the current server date and time.
<?php// Get the current year (4 digits)$year = date('Y');echo "It is $year"; // Will output: It is 2026?>What's happening here? The character Y is a format that means "year in 4-digit format". If you need the year as the last two digits (24, 25, 26), use y:
<?phpecho date('y'); // 26?>Why is date('Y') good?
- Simplicity: one line of code and it's done.
- Flexibility: can be combined with other formats:
date('Y-m-d')— date,date('Y-m-d H:i:s')— date with time. - Speed: the function works very quickly, no object creation required.
When should you be careful?
The date() function uses the timezone set in the PHP settings (the date.timezone directive in php.ini). If the server has the wrong timezone set, getting the current year in PHP might give an incorrect result on New Year's Eve.
Solution: always set the timezone at the beginning of the script:
<?phpdate_default_timezone_set('Europe/Moscow');echo date('Y'); // Now definitely 2026?>Method #2: Object-Oriented Approach — The DateTime Class
If you are writing modern PHP code (version 5.2+), it's better to use the DateTime class. This is a more powerful and flexible tool for working with dates.
<?php$now = new DateTime();$year = $now->format('Y');echo "Current year: $year";?>Advantages of DateTime:
- Object model: easily manipulate dates (add days, months, compare).
- Clear timezone management: pass the timezone to the constructor.
- Internationalization: easily localize dates.
Example with timezone:
<?php$timezone = new DateTimeZone('Europe/London');$now = new DateTime('now', $timezone);echo $now->format('Y'); // Year in Greenwich Mean Time?>Method #3: Using the strftime() Function (Deprecated, but Still Alive)
The strftime() function was once the standard for localized date output. It has been deprecated as of PHP 8.1, but you may encounter it in older projects.
<?phpsetlocale(LC_TIME, 'ru_RU.UTF-8');echo strftime('%Y'); // 2026?>Important: do not use strftime() in new projects. Instead, use IntlDateFormatter for localization.
Method #4: How to Get the Year from an Arbitrary Date?
Often you need not just to get the current year in PHP, but to extract it from an existing date