Introduction: why is the "current year" question so popular?
\n\nIt would seem that nothing could be simpler than asking PHP: "Hey, what year is it?" — and getting an answer. But it's not that simple! The question "How do I use PHP to get the current year?" has gathered tens of thousands of views on Stack Overflow. Why?
\n\nFirstly, this is one of the first tasks a beginner faces: displaying "© 2024" in the site footer, creating a dynamic year list in a registration form, or simply checking a user's age. Secondly, PHP offers several different approaches, and each has its own nuances. Thirdly (and this is the most insidious), beginners often confuse date('y') and date('Y') — and get 24 instead of 2024. Then they wonder why the site is "living" in the last century.
In this article, we'll cover all the popular ways to get the current year in PHP, from the most obvious to the more advanced. You'll learn how to avoid typical mistakes, which method to choose for your project, and how to work with the year in different time zones. Let's go!
\n\n1. The simplest way: the date() function
\n\nThe king of all PHP functions for working with dates is date(). It takes a format and optionally a timestamp. If no timestamp is specified, the current server time is used.
How to get a 4-digit year (2024, 2025…)
\n<?php\necho date('Y'); // Outputs: 2024\n?>\n\nThe character Y (uppercase) is the full year representation, 4 digits. This is what you should use in 99% of cases.
\n\nHow to get a 2-digit year (24, 25…)
\n<?php\necho date('y'); // Outputs: 24\n?>\n\nThe character y (lowercase) is the two-digit year. Use it only if you are writing a toy program or working with legacy code (which is probably already broken due to Y2K-like issues).
\n\nReal example: copyright in the footer
\n<footer>\n <p>© 2010–<?php echo date('Y'); ?> My super site</p>\n</footer>\n\nThis code will automatically update the year in the footer — you won't have to edit it every January. Cool, right?
\n\n2. Object-oriented approach: the DateTime class
\n\nIf you are writing modern PHP (7.x, 8.x), it's better to use the DateTime class. It is more flexible, convenient for manipulations, and easier to read.
Getting the year via DateTime
\n<?php\n$now = new DateTime();\necho $now->format('Y'); // 2024\n?>\n\nThe format() method works the same as date(), but on an object. The advantage is that you can easily change the date, add days, months, and then format it again.
Extended example: year from a specific date
\n<?php\n$date = new DateTime('2023-03-15');\necho $date->format('Y'); // 2023\n?>\n\nThis is useful when you are working with a database and storing dates as strings.
\n\n3. The good old strftime() (but with a caveat)
\n\nThe strftime() function is another way to get the year. It is good because it takes into account the locale (language and regional settings). For example, in the Russian locale, it can output the month name in Russian.
Example with strftime
\n<?php\nsetlocale(LC_TIME, 'ru_RU.utf8');\necho strftime('%Y'); // 2024\n?>\n\nBut there is an important nuance: starting with PHP 8.1, the strftime() function is marked as deprecated. It can still be used, but it may be removed in future versions. If you are writing new code, it's better to use DateTime.
4. The $_SERVER['REQUEST_TIME'] variable
\n\nSometimes you need to get the year "on the fly" without creating objects. For this, you can use the superglobal array $_SERVER.
Blogs
Book Recommendations
Read also
Server-Sent Events in JavaScript: Real-Time Without WebSocket
Working with the File System in C++: A Complete Guide for Beginners
How to Get a Job as a Java Developer: Tips and a Resume in Practice
Go in 2025: Top Books for Juniors and Seniors. What to Read?
GraphQL vs REST in TypeScript: What to Choose for Your Project