How do I use PHP to get the current year?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
To get the current year in PHP, call date("Y"). This returns the four-digit year as a string based on the server's configured timezone. For timezone-aware results, use the DateTime class with an explicit DateTimeZone.
The date() Function
The date() function is the most direct way to get the current year. The format character Y produces a four-digit year, while y produces a two-digit year.
The function signature accepts an optional Unix timestamp. When omitted, it uses the current time:
Using the DateTime Class
The DateTime class provides an object-oriented approach with better timezone handling and more flexibility for date arithmetic:
You can also create a DateTime from a specific date string:
The immutable variant DateTimeImmutable prevents accidental modification when passing date objects around:
Timezone Handling
PHP date functions use the server's default timezone unless you specify one explicitly. This is the most common source of bugs when your server is in one timezone and your users are in another.
Setting the Default Timezone
You can set the timezone globally with date_default_timezone_set() or in php.ini:
Per-Instance Timezone with DateTime
The DateTime class accepts a DateTimeZone object, which lets you handle multiple timezones without changing the global default:
This matters on New Year's Eve. At 11 PM UTC on December 31, Tokyo is already in the next year (8 AM January 1), while New York is still in the current year (6 PM December 31).
Real-World Usage Patterns
Dynamic Copyright Footer
The most common use case is a copyright notice that updates automatically:
Year Dropdown for Forms
Generating a range of years for a date-of-birth selector:
Filtering Records by Year
Using the current year in a database query:
Age Calculation
Computing age from a birth year:
Comparison of Methods
| Method | Timezone Control | Immutability | Best For |
date("Y") | Global default only | N/A | Simple one-liner, copyright footers |
DateTime | Per-instance via DateTimeZone | Mutable | Date arithmetic, complex logic |
DateTimeImmutable | Per-instance via DateTimeZone | Immutable | Functional style, passing dates safely |
strftime() | Global default only | N/A | Deprecated in PHP 8.1, avoid |
Carbon (library) | Per-instance | Both variants | Laravel projects, fluent API |
Format Characters Reference
A quick reference for the most useful year and date format characters:
| Character | Output | Example |
Y | Four-digit year | 2026 |
y | Two-digit year | 26 |
L | Leap year (1 or 0) | 0 |
m | Month with leading zero | 06 |
n | Month without leading zero | 6 |
d | Day with leading zero | 18 |
j | Day without leading zero | 18 |
G | Hour (24h, no leading zero) | 14 |
i | Minutes with leading zero | 05 |
Common Pitfalls
Assuming the server timezone matches the user's timezone. If your server is in UTC and your user is in Tokyo, date("Y") can return the wrong year around midnight on December 31. Always use explicit timezones for user-facing dates.
Using strftime() in PHP 8.1+. The strftime() function was deprecated in PHP 8.1 and removed in PHP 9. Use date() or DateTime::format() instead. If you are maintaining legacy code that uses strftime("%Y"), migrate to date("Y").
Hardcoding the year. It sounds obvious, but hardcoded years in copyright notices, license headers, and configuration files are still common. Always generate the year dynamically.
Forgetting that date() returns a string. If you need an integer for arithmetic, cast explicitly: $year = (int) date("Y");. PHP's type juggling often handles this implicitly, but explicit casting prevents surprises.
Not setting a default timezone. If date.timezone is not set in php.ini and you do not call date_default_timezone_set(), PHP emits an E_WARNING and falls back to UTC. Set the timezone explicitly in every project.
Summary
Use date("Y") for a quick four-digit year string in PHP. For timezone-aware code, prefer the DateTime or DateTimeImmutable class with an explicit DateTimeZone. Always set your application's timezone explicitly rather than relying on the server default. Cast to int when you need the year for arithmetic. Avoid strftime() in PHP 8.1+ since it is deprecated. For production applications, the DateTimeImmutable class combined with a per-instance timezone gives you the most predictable behavior.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.