
Working with dates is a common task in web development, and sometimes, you may need to display the year in a 2-digit format (like 24
instead of 2024
) in PHP.
In this article, we'll walk through different methods to achieve this, along with step-by-step explanations, code snippets, a complete code example, and important tips to avoid common mistakes.
Let's dive right in!
Step-by-Step Explanation
PHP provides several ways to format dates. The easiest and most reliable way to get a 2-digit year is by using the date() function.
✅ Method 1: Using date()
Function
The date()
function in PHP formats a local date and time according to a specified format.
-
To get a 2-digit year, use the format character
y
(lowercase y).
Code Snippet:
<?php
echo date("y");
?>
Explanation:
-
"y"
returns the year as two digits (e.g.,24
for 2024,99
for 1999). -
date()
uses the current server date and time unless otherwise specified.
✅ Method 2: Using DateTime
Object
If you're working with objects or need more flexibility (like formatting a specific date), using the DateTime class is a modern and preferred approach.
Code Snippet:
<?php
$date = new DateTime();
echo $date->format("y");
?>
Explanation:
-
new DateTime()
creates a new DateTime object with the current date and time. -
format("y")
gives the 2-digit year.
✅ Method 3: Formatting a Specific Date
You might not always want the current year. You can easily format a custom date:
Code Snippet:
<?php
$date = new DateTime('2001-05-21');
echo $date->format('y'); // Output: 01
?>
Explanation:
-
You pass a custom date string into
new DateTime('YYYY-MM-DD')
. -
Then format it to display only the two-digit year.
Complete Example: Full Program
Here's a full PHP script demonstrating all three methods:
<?php
// Method 1: Using date() function
echo "Current 2-digit year using date(): " . date("y") . "<br>";
// Method 2: Using DateTime object
$today = new DateTime();
echo "Current 2-digit year using DateTime: " . $today->format("y") . "<br>";
// Method 3: Using a specific date
$specificDate = new DateTime('1995-08-15');
echo "2-digit year for specific date (1995-08-15): " . $specificDate->format("y") . "<br>";
?>
Sample Output:
Current 2-digit year using date(): 24
Current 2-digit year using DateTime: 24
2-digit year for specific date (1995-08-15): 95
Tips for Working with 2-Digit Years
-
Prefer
DateTime
when you are working with user inputs, database dates, or require timezone adjustments. -
Use lowercase
y
for two digits and uppercaseY
for four digits:-
date("y")
→24
-
date("Y")
→2024
-
-
Always sanitize external date inputs if they come from users.
Common Pitfalls to Avoid
-
Using the wrong format character:
Be careful — usingY
instead ofy
will give you a 4-digit year!echo date("Y"); // Outputs 2024, not 24
-
Assuming server timezone is correct:
By default, PHP uses the server's timezone setting. If your application is timezone-sensitive, set it explicitly:date_default_timezone_set('America/New_York');
-
Empty or incorrect date input:
If you are formatting a custom date and it's wrong or empty,DateTime
might throw an error. Always validate your input before creating aDateTime
object.
Summary
Method | Description | Example Code |
---|---|---|
date("y") |
Quick and simple for the current date | echo date("y"); |
DateTime Object |
Best for advanced usage and custom dates | $date->format("y"); |
Specific Date | Format a particular date | new DateTime('1995-08-15') |
Whether you need the current year's last two digits or want to format any specific date neatly, PHP makes it simple with its powerful date handling functions.
✅ Now you know exactly how to get the year in 2 digits in PHP — along with pro tips to keep your code clean and error-free!
Would you also like me to show how to automatically handle leading zeros or output different separators (like slash /
instead of space)?
I can add that too if you want!