Problem:
You have a date which consists of day, month, and year. You just want to retrieve the year from that date.
Solution:
There are few ways you can retrieve year from a date. In the following you’ll see different ways to retrieve year from current date or from any date –
Method 1: Using only date() function to get current year
PHP’s date() function can let you know date and time related information based on the formatting string it takes in its first parameter. It can take two parameters. If you use only one parameter, It will return info about the current time.
Depending on the formatting parameter you can get two types of output as the year-
- Y- A four digit numeric representation of a year. Ex. 2001
- y – A two digit numeric representation of a year. Ex. 01
Now, see these in action in the example-
<?php echo "4 digit of current year is: " . date("Y"); echo "<br />"; echo "2 digit of current year is: " . date("y"); ?>
[wpdm_file id=151]
Output:
4 digit of current year is: 2014
2 digit of current year is: 14
Method 2: Using strtotime() and date() function to get year from any date
In this method, to get the year, we’ll follow two steps-
- We’ll first convert a date to its equivalent timestamp using strtotime() function. A timestamp of a date is the representation of seconds from January 1 1970 00:00:00 UTC to that time, then,
- We’ll use the date() function that we used in the method 1.
See how it works in action in the following example-
<?php $timestamp = strtotime("5th February 1993"); echo "4 digit of current year is: " . date("Y", $timestamp); echo "<br />"; echo "2 digit of current year is: " . date("y", $timestamp); ?>
[wpdm_file id=152]
Output:
4 digit of current year is: 1993
2 digit of current year is: 93
Method 3: Using DateTime class to get current year
From PHP 5.2, PHP provides some ready-made classes to solve daily problems. One of the classes is DateTime which helps to solve date time related issues.
To get current year using DateTime class, we’ll follow two steps-
- Create an object of DateTime class. The DateTIme() class represents the current time.
- Then, use format() method to retrieve the year from the newly created object.
See the following example-
<?php $now = new DateTime(); echo "4 digit of current year is: " . $now->format('Y'); echo "<br />"; echo "2 digit of current year is: " . $now->format('y'); ?>
[wpdm_file id=153]
Output:
4 digit of current year is: 2014
2 digit of current year is: 14
Method 4: Using CreateFromFormat() method to get year from any date
In this method, we’ll retrieve year from any date in two steps-
- First, we’ll create a new DateTime object from the date using createFromFormat() method, then
- Then, we’ll retrieve the year using format() method
See it in action in the following example –
<?php $dateObj = DateTime::createFromFormat('jS F Y', '5th February 1993'); echo "4 digit of current year is: " . $dateObj->format('Y'); echo "<br />"; echo "2 digit of current year is: " . $dateObj->format('y'); ?>
[wpdm_file id=154]
Output:
4 digit of current year is: 1993
2 digit of current year is: 93