How to Get the Current Year in PHP

  1. Using date() function to get the current year in PHP
  2. Using strftime() function to get the current year in PHP
  3. Using DateTime object 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

Using 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 tells about 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.

Using 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 variable format tells about the format of the date or time. It is a string.

The variable 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.

Using 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

  • How to get the last day of the month with PHP functions
  • How to calculate the difference between two dates using PHP
  • How to convert DateTime to string in PHP
  • How to Convert a Date to a Timestamp in PHP
  • How to Convert a Timestamp to a Readable Date or Time in PHP
  • comments powered by Disqus