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; ?>
- Here, you can see how we use the date function to build PHP code to obtain the current year.
- The date() function that comes with PHP is used at the beginning of the code to retrieve the current year.
- 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.
- The $year variable is used to keep the string representation of the current year that the function returns.
- The $year variable's value is displayed on the screen in the final line using the echo statement.
- 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; ?>
- Here, you can see how to use the DateTime object to build PHP code to retrieve the current year.
- This code is written to display the current year in Mumbai time.
- The "Asia/Kolkata" time zone, which is the time zone for Mumbai, is established using the DateTimeZone class.
- The DateTimeZone class is then instantiated using the new keyword.
- The date and time in Mumbai are used to construct a new instance of the DateTime object using the DateTime class.
- The DateTime class's constructor accepts the now parameter in order to obtain the time in the chosen time zone.
- 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.
- In the end, the current year for the Mumbai time zone is output using the echo statement.
- 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.