How to get the year in 2 digits in php?

How to get the year in 2 digits in php?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Two-Digit Years in PHP and Laravel

The standard date() function in PHP returns a full four-digit year by default when using the Y format character. To obtain a two-digit representation, such as converting 2015 to 15, you simply swap that character for y. This change is case-sensitive and dictates exactly how the value is formatted.

echo date("y"); // Outputs: 15 (for year 2015)

Using lowercase y tells PHP to extract only the last two digits of the current year. If you require a specific format where the century prefix is omitted entirely, this method works perfectly. However, relying solely on this formatting string can lead to ambiguity if your application logic depends on knowing whether the year refers to the current decade or a historical one.

Consider a scenario where you are parsing user input or handling legacy data. If a user submits 15, PHP cannot distinguish between 2015 and 1915 without additional context. While this ambiguity exists in any system using two-digit years, it is a critical design consideration for financial applications or systems managing long-term records. Always ensure that the downstream logic handling these values explicitly defines the century or rejects inputs outside an acceptable range to prevent data integrity issues.

Contextual Formatting with Laravel

If you are working within a Laravel application, the approach shifts slightly depending on whether you are using Blade templates or PHP code blocks directly in controllers. The underlying date() function remains consistent, but Laravel provides higher-level abstractions that can simplify access to formatted dates through helper functions and formatting strings.

In Blade templates, you can use the {{ $now->format('y') }} syntax to achieve the same result without calling the global date() function directly in your logic layer. This keeps your controllers cleaner by pushing presentation concerns into the view or using Eloquent model casts.

// In a Blade template or within a method returning a string
echo now()->format('y'); 

Laravel's Carbon instance (accessed via the now() helper) offers extensive formatting options that mirror PHP's DateTime class behavior. It is worth noting that while date('Y') returns 2015, using date('o') provides the ISO 8601 year, which can differ in leap years but generally aligns with the four-digit representation for standard calendar years. For most web applications, date('y') remains the idiomatic choice for short dates like "15".

Avoiding Common Pitfalls

A frequent mistake developers make is confusing the uppercase Y and lowercase y format characters. The uppercase version (Y) always returns a four-digit string ranging from 0000 to 9999, while the lowercase version (y) truncates this to two digits. This distinction is subtle but essential for ensuring consistent output across different locales and system configurations.

Another pitfall involves timezone handling. The date() function uses the timezone set in your server's configuration or via the date_default_timezone_set() function. If you extract a two-digit year without setting a default timezone, PHP may throw a warning if it cannot determine the current time, particularly in environments where the system clock is misconfigured. Always ensure your application runs with a valid timezone to guarantee predictable results when formatting temporal data.

When integrating these formats into APIs or JSON responses, remember that client-side display logic often dictates whether two digits are sufficient. Front-end frameworks might automatically interpret "15" as 2015 based on the current decade, but this assumption breaks if you ever need to reference a year from the previous century. Design your data layer to store full four-digit years whenever possible, applying the two-digit truncation only at the presentation layer.

Our network