All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Current Year In PHP

Last Updated : Mar 11, 2024

Get Current Year In PHP

In this article we will show you the solution of get current year in PHP, it's a regular chore in many online applications to obtain the current year. The date() function in PHP can be used to retrieve the current year in a particular format.

Based on the format string provided as the first argument, the date() function provides the string representation of the current date and time.

You can instruct the date() function to return the current year by passing it the format string "Y".

The four-digit year is represented by the "Y" format character. Using PHP's DateTime & DateTimeZone classes is another technique to obtain the current year.

Particularly when dealing with several time zones, these classes offer more flexibility when working with dates and hours.

The format() function of the DateTime class can be used to generate a new instance and retrieve the current year.

Using the DateTimeZone class, the time zone can be changed to any location in the world. We’ll talk about the PHP idea of getting the current year.

Step By Step Guide On Get Current Year In PHP :-

Method - Applying the date Function

<?php
$year = date("Y");
echo $year;
?>
  1. Here, you can see how we use the date function to build PHP code to obtain the current year.
  2. The date() function that comes with PHP is used at the beginning of the code to retrieve the current year.
  3. The function accepts "Y" as its input in place of the format parameter. Four numbers are used to represent the year in the "Y" format.
  4. The $year variable is used to keep the string representation of the current year that the function returns.
  5. The $year variable's value is displayed on the screen in the final line using the echo statement.
  6. As a result, the current year will be printed as a number with four digits when this code is run.

Method - Applying the DateTime Object

<?php
$timezone = new DateTimeZone('Asia/Kolkata');
$date = new DateTime('now', $timezone);
$year = $date->format('Y');
echo "The current year in Mumbai is: " . $year;
?>
  1. Here, you can see how to use the DateTime object to build PHP code to retrieve the current year.
  2. This code is written to display the current year in Mumbai time.
  3. The "Asia/Kolkata" time zone, which is the time zone for Mumbai, is established using the DateTimeZone class.
  4. The DateTimeZone class is then instantiated using the new keyword.
  5. The date and time in Mumbai are used to construct a new instance of the DateTime object using the DateTime class.
  6. The DateTime class's constructor accepts the now parameter in order to obtain the time in the chosen time zone.
  7. The DateTime object can be used to retrieve the current year in the format "Y" using the format() function. The four-digit year is represented by this format character.
  8. In the end, the current year for the Mumbai time zone is output using the echo statement.
  9. The message string and the value of the $year variable are concatenated using the. operator.

Conclusion :-

As a result, we were able to understand how to use PHP's get current year idea

We also discovered that there are several ways to obtain the current year in PHP.

The simplest method is to utilise the built-in date() function, which, based on the supplied format parameter, returns the current year.

The DateTime class also allows you to interact with time zones and retrieve the current year in a particular time zone.

I hope this article on get current year in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪