How to Get the Current Year in PHP

Minahil Noor Feb 02, 2024
  1. Use date() Function to Get the Current Year in PHP
  2. Use strftime() Function to Get the Current Year in PHP
  3. Use DateTime Object to Get the Current Year in PHP
How to Get the Current Year in PHP

In this article, we will introduce methods to get the current year.

  • Using date() function
  • Using strftime() function
  • Using format() method in the DateTime object

Use date() Function to Get the Current Year in PHP

We could use the built-in date() function to get the current year. It returns the current date with the specified format. Its syntax is as follows

date($format, $timestamp);

The parameter $format tells about the final date format that will be displayed. It has several variations. It should be a string.

The parameter $timestamp is optional. It specifies the specific timestamp and displays the date accordingly. If no timestamp is passed, then it uses the current date by default.

<?php
$Date = date("d-m-Y");  
echo "The current date is $Date.";
echo "\n";
$Year = date("Y");
echo "The current year is $Year.";
echo "\n";
$Year2 = date("y");
echo "The current year in two digits is $Year2.";
?>

The format "d-m-Y" will return the full date with 4 digit year value. The format "Y" will return the 4 digit year value. The format "y" will return the 2 digit year value. You can modify it according to your requirements.

Output:

The current date is 20-04-2020.
The current year is 2020.
The current year in two digits is 20.

Use strftime() Function to Get the Current Year in PHP

Another built-in function in PHP - strftime() could also return the date or time string in the given format. It has a different format for variations of date and time.

strftime($format, $timestamp);

The parameter $format tells about the format of the date or time. It is a string.

The parameter $timestamp is an optional variable that tells about the UNIX timestamp. It is an integer. If not given, the function returns the current date or time.

<?php
$Date = strftime("%d-%m-%Y");  
echo "The current date is $Date.";
echo "\n";
$Year = strftime("%Y"); 
echo "The current year is $Year.";
echo "\n";
$Year2 = strftime("%y"); 
echo "The current year in two digits is $Year2.";
?>
Warning

We can get the current year in 4 digits value by using format "%Y", and in 2 digits value by using format "%y".

Output:

The current date is 20-04-2020.
The current year is 2020.
The current year in two digits is 20.

Use DateTime Object to Get the Current Year in PHP

In PHP, DateTime format() function is used to display the date in a specified format.

$ObjectName = new DateTime();
$ObjectName->format($format); 

The parameter $format specifies the format of the date.

<?php
$Object = new DateTime();  
$Date = $Object->format("d-m-Y");  
echo "The current date is $Date.";
echo "\n";
$Year = $Object->format("Y"); 
echo "The current year is $Year.";
echo "\n";
$Year2 = $Object->format("y"); 
echo "The current year in two digits is $Year2.";
?>

The output is the date with the specified format.

Output:

The current date is 20-04-2020.
The current year is 2020.
The current year in two digits is 20.

Related Article - PHP DateTime