How to get the current year in PHP? 5 simple ways

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

Introduction: why is the "current year" question so popular?

\n\n

It 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\n

Firstly, 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.

\n\n

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\n

1. The simplest way: the date() function

\n\n

The 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.

\n\n

How to get a 4-digit year (2024, 2025…)

\n
<?php\necho date('Y'); // Outputs: 2024\n?>
\n\n

The character Y (uppercase) is the full year representation, 4 digits. This is what you should use in 99% of cases.

\n\n

How to get a 2-digit year (24, 25…)

\n
<?php\necho date('y'); // Outputs: 24\n?>
\n\n

The 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\n

Real example: copyright in the footer

\n
<footer>\n    <p>&copy; 2010–<?php echo date('Y'); ?> My super site</p>\n</footer>
\n\n

This code will automatically update the year in the footer — you won't have to edit it every January. Cool, right?

\n\n

2. Object-oriented approach: the DateTime class

\n\n

If 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.

\n\n

Getting the year via DateTime

\n
<?php\n$now = new DateTime();\necho $now->format('Y'); // 2024\n?>
\n\n

The 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.

\n\n

Extended example: year from a specific date

\n
<?php\n$date = new DateTime('2023-03-15');\necho $date->format('Y'); // 2023\n?>
\n\n

This is useful when you are working with a database and storing dates as strings.

\n\n

3. The good old strftime() (but with a caveat)

\n\n

The 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.

\n\n

Example with strftime

\n
<?php\nsetlocale(LC_TIME, 'ru_RU.utf8');\necho strftime('%Y'); // 2024\n?>
\n\n

But 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.

\n\n

4. The $_SERVER['REQUEST_TIME'] variable

\n\n

Sometimes you need to get the year "on the fly" without creating objects. For this, you can use the superglobal array $_SERVER.

\n\n


                
                

Blogs

Book Recommendations