PHP
Current Year
Coding
Web Development
Programming

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.

Browse interview questions

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.

php
1// Four-digit year: "2026"
2echo date("Y");
3
4// Two-digit year: "26"
5echo date("y");

The function signature accepts an optional Unix timestamp. When omitted, it uses the current time:

php
1// Year from a specific timestamp
2$timestamp = mktime(0, 0, 0, 1, 1, 2020);
3echo date("Y", $timestamp); // "2020"
4
5// Current year (no timestamp argument)
6echo date("Y"); // current year

Using the DateTime Class

The DateTime class provides an object-oriented approach with better timezone handling and more flexibility for date arithmetic:

php
$now = new DateTime();
echo $now->format("Y"); // "2026"

You can also create a DateTime from a specific date string:

php
$date = new DateTime("2025-06-15");
echo $date->format("Y"); // "2025"

The immutable variant DateTimeImmutable prevents accidental modification when passing date objects around:

php
1$now = new DateTimeImmutable();
2echo $now->format("Y");
3
4// modify() returns a new instance instead of changing $now
5$nextYear = $now->modify("+1 year");
6echo $nextYear->format("Y");
7echo $now->format("Y"); // unchanged

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:

php
1// In your script
2date_default_timezone_set("America/New_York");
3echo date("Y"); // year according to Eastern Time
4
5// In php.ini
6// date.timezone = "America/New_York"

Per-Instance Timezone with DateTime

The DateTime class accepts a DateTimeZone object, which lets you handle multiple timezones without changing the global default:

php
1$utc = new DateTime("now", new DateTimeZone("UTC"));
2echo $utc->format("Y-m-d H:i:s T");
3
4$tokyo = new DateTime("now", new DateTimeZone("Asia/Tokyo"));
5echo $tokyo->format("Y-m-d H:i:s T");
6
7$ny = new DateTime("now", new DateTimeZone("America/New_York"));
8echo $ny->format("Y-m-d H:i:s T");

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

The most common use case is a copyright notice that updates automatically:

php
<footer>
    &copy; <?php echo date("Y"); ?> Acme Corp. All rights reserved.
</footer>

Year Dropdown for Forms

Generating a range of years for a date-of-birth selector:

php
1$currentYear = (int) date("Y");
2$startYear = $currentYear - 100;
3
4echo '<select name="birth_year">';
5for ($y = $currentYear; $y >= $startYear; $y--) {
6    echo "<option value=\"$y\">$y</option>";
7}
8echo '</select>';

Filtering Records by Year

Using the current year in a database query:

php
$year = date("Y");
$stmt = $pdo->prepare("SELECT * FROM orders WHERE YEAR(created_at) = :year");
$stmt->execute(["year" => $year]);

Age Calculation

Computing age from a birth year:

php
1$birthDate = new DateTime("1990-03-15");
2$now = new DateTime();
3$age = $now->diff($birthDate)->y;
4echo "Age: $age years";

Comparison of Methods

MethodTimezone ControlImmutabilityBest For
date("Y")Global default onlyN/ASimple one-liner, copyright footers
DateTimePer-instance via DateTimeZoneMutableDate arithmetic, complex logic
DateTimeImmutablePer-instance via DateTimeZoneImmutableFunctional style, passing dates safely
strftime()Global default onlyN/ADeprecated in PHP 8.1, avoid
Carbon (library)Per-instanceBoth variantsLaravel projects, fluent API

Format Characters Reference

A quick reference for the most useful year and date format characters:

CharacterOutputExample
YFour-digit year2026
yTwo-digit year26
LLeap year (1 or 0)0
mMonth with leading zero06
nMonth without leading zero6
dDay with leading zero18
jDay without leading zero18
GHour (24h, no leading zero)14
iMinutes with leading zero05

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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions