In this tutorial, we will discuss on how to get current year in PHP. The Date function is an inbuilt function that is used to format the timestamp.
There are different ways to get the current year in PHP which is easy to read and understand.
The Syntax to Get Current Year in PHP
In the code below is the method we will use to get the current year.
date( format, timestamp )
In the date function the format defines the returned date and time while the timestamp(Unix Timestamp) it is optional and the defaults value of time and if not included the current date and time can be used.
Note: The Unix Timestamps cannot manage the Time zones. The DateTimeImmutable and DateTimeInterface::format() can be used to format the date/time details with a time zone attached.
Parameters Used to Get the Current Year PHP
We have two parameters which are the format and timestamp.
- Format – It is accepted by the DateTimeInterface::format().
- Timestamp – It is the optional parameter of a Unix timestamp that can be shows defaults for the current local time
How to get current year in PHP?
Through using a PHP function which is just called
date() function and the format will be Y. Capital Y will become the four digit year.
Example to get the current year in PHP
Using the date() function in PHP, you may add the “Y” format character as follows to get the current year. Let’s see the example below.
<?php echo "Current Year is :"; $year_now = date("Y"); echo $year_now ; ?>
Test and run the code above now in this Online PHP Compiler.
If we run the code example above the output will show the four digits of the current year.
To retrieve the format of 2 last digit of the year we can use the format small letter or lowercase “y”. Let’s see the example below.
<?php echo "Current Year is :"; $year_now = date("y"); echo $year_now ; ?>
If we will run the example code above the result will show the last two digits of the year which is like this below:
Date() Function Format Available
The listing below is the available format of the date() function that include multiple characters to allow the various formats to generate dates.
- y – The lowercase “y” shows the two last digits of the year. For example 21 or 22.
- Y – The uppercase “Y” shows the four digits of a year. For example 2021 or 2022.
- m – The lowercase “m” shows the month of numbers with start of number 01 or 12.
- M – The uppercase “M” shows the month of a text and is shortened to (“Jan to Dec“).
- d – The lowercase “d” shows the day of the month. It starts with the two digits which are (01 to 31).
- D – The uppercase “D” shows the day of the week in a text. It starts with the (Mon to Sun).
In conclusion, we already solve how to get the current year in PHP by using the date function and by providing an example with output. We also provide the syntax, parameters and the format available for the PHP code for current year.
So there you have it! These tutorials are good for the beginners in developing their skills in programming web applications.