- How to Get the Current Year with PHP
- Applying the date Function
- Applying the DateTime Object
- date
- Parameters
- Return Values
- Errors/Exceptions
- Changelog
- Examples
- Notes
- See Also
- User Contributed Notes
- Get the Current Year in PHP
- Use date() Function to Get the Current Year in PHP
- Use strftime() Function to Get the Current Year in PHP
- Use DateTime Object to Get the Current Year in PHP
- Related Article — PHP DateTime
- How to Get Year From a Date in PHP?
- Problem:
- Solution:
- Method 1: Using only date() function to get current year
- Method 2: Using strtotime() and date() function to get year from any date
- Method 3: Using DateTime class to get current year
- Method 4: Using CreateFromFormat() method to get year from any date
How to Get the Current Year with PHP
Here, you will find comprehensive information about one of the most common issues in PHP: how to get the current year.
Below you can find the main functions, allowing you to do that with PHP.
Applying the date Function
To get the current year with PHP date function, it is necessary to pass in the Y format as follows:
//Get the current year with //PHP's date function. $year = date("Y"); echo $year; ?>
This example prints out current year’s full four-digit representation.
For getting a two-digit format, apply the lowercase y format character, like so:
Applying the DateTime Object
In this section, you will find a more object-oriented approach, using the DateTime object as follows:
//apply DateTime. $currentDate = new DateTime(); //Get the year by using the format method. $year = $currentDate->format("Y"); //Printing that out echo $year; ?>
So, the DateTime object is generated, and you can retrieve the current year via passing the Y character as a $format parameter.
For getting a two-digit format, using the y lowercase character is necessary.
//Receiving the two-digit format of //the current year withDateTime $currentDate = new DateTime(); $year = $currentDate->format("y"); echo $year; ?>
date
Returns a string formatted according to the given format string using the given integer timestamp (Unix timestamp) or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time() .
Unix timestamps do not handle timezones. Use the DateTimeImmutable class, and its DateTimeInterface::format() formatting method to format date/time information with a timezone attached.
Parameters
Note: date() will always generate 000000 as microseconds since it takes an int parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds.
The optional timestamp parameter is an int Unix timestamp that defaults to the current local time if timestamp is omitted or null . In other words, it defaults to the value of time() .
Return Values
Returns a formatted date string.
Errors/Exceptions
Every call to a date/time function will generate a E_WARNING if the time zone is not valid. See also date_default_timezone_set()
Changelog
Version | Description |
---|---|
8.0.0 | timestamp is nullable now. |
Examples
Example #1 date() examples
// set the default timezone to use.
date_default_timezone_set ( ‘UTC’ );
?php
// Prints something like: Monday
echo date ( «l» );
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date ( ‘l jS \of F Y h:i:s A’ );
// Prints: July 1, 2000 is on a Saturday
echo «July 1, 2000 is on a » . date ( «l» , mktime ( 0 , 0 , 0 , 7 , 1 , 2000 ));
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date ( DATE_RFC2822 );
// prints something like: 2000-07-01T00:00:00+00:00
echo date ( DATE_ATOM , mktime ( 0 , 0 , 0 , 7 , 1 , 2000 ));
?>
You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash. If the character with a backslash is already a special sequence, you may need to also escape the backslash.
Example #2 Escaping characters in date()
It is possible to use date() and mktime() together to find dates in the future or the past.
Example #3 date() and mktime() example
$tomorrow = mktime ( 0 , 0 , 0 , date ( «m» ) , date ( «d» )+ 1 , date ( «Y» ));
$lastmonth = mktime ( 0 , 0 , 0 , date ( «m» )- 1 , date ( «d» ), date ( «Y» ));
$nextyear = mktime ( 0 , 0 , 0 , date ( «m» ), date ( «d» ), date ( «Y» )+ 1 );
?>?php
Note:
This can be more reliable than simply adding or subtracting the number of seconds in a day or month to a timestamp because of daylight saving time.
Some examples of date() formatting. Note that you should escape any other characters, as any which currently have a special meaning will produce undesirable results, and other characters may be assigned meaning in future PHP versions. When escaping, be sure to use single quotes to prevent characters like \n from becoming newlines.
Example #4 date() Formatting
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
?php
$today = date ( «F j, Y, g:i a» ); // March 10, 2001, 5:16 pm
$today = date ( «m.d.y» ); // 03.10.01
$today = date ( «j, n, Y» ); // 10, 3, 2001
$today = date ( «Ymd» ); // 20010310
$today = date ( ‘h-i-s, j-m-y, it is w Day’ ); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date ( ‘\i\t \i\s \t\h\e jS \d\a\y.’ ); // it is the 10th day.
$today = date ( «D M j G:i:s T Y» ); // Sat Mar 10 17:16:18 MST 2001
$today = date ( ‘H:m:s \m \i\s\ \m\o\n\t\h’ ); // 17:03:18 m is month
$today = date ( «H:i:s» ); // 17:16:18
$today = date ( «Y-m-d H:i:s» ); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>
To format dates in other languages, IntlDateFormatter::format() can be used instead of date() .
Notes
Note:
To generate a timestamp from a string representation of the date, you may be able to use strtotime() . Additionally, some databases have functions to convert their date formats into timestamps (such as MySQL’s » UNIX_TIMESTAMP function).
Timestamp of the start of the request is available in $_SERVER[‘REQUEST_TIME’] .
See Also
- DateTimeImmutable::__construct() — Returns new DateTimeImmutable object
- DateTimeInterface::format() — Returns date formatted according to given format
- gmdate() — Format a GMT/UTC date/time
- idate() — Format a local time/date part as integer
- getdate() — Get date/time information
- getlastmod() — Gets time of last page modification
- mktime() — Get Unix timestamp for a date
- IntlDateFormatter::format() — Format the date/time value as a string
- time() — Return current Unix timestamp
- Predefined DateTime Constants
User Contributed Notes
- Date/Time Functions
- checkdate
- date_add
- date_create_from_format
- date_create_immutable_from_format
- date_create_immutable
- date_create
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse_from_format
- date_parse
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- date
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get
Get the Current Year in PHP
- Use date() Function to Get the Current Year in PHP
- Use strftime() Function to Get the Current Year in PHP
- Use 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
Use 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
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 specifies 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.
The current date is 20-04-2020. The current year is 2020. The current year in two digits is 20.
Use 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.
The parameter $format tells about the format of the date or time. It is a string.
The parameter $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."; ?>
We can get the current year in 4 digits value by using format «%Y» , and in 2 digits value by using format «%y» .
The current date is 20-04-2020. The current year is 2020. The current year in two digits is 20.
Use 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.
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 Year From a Date in PHP?
New here? Like SchoolsOfWeb on Facebook to stay up to date with new posts.
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-
"; echo "2 digit of current year is: " . date("y"); ?>
Output:
4 digit of current year is: 2014
2 digit of current year is: 14Method 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-
"; echo "2 digit of current year is: " . date("y", $timestamp); ?>
Output:
4 digit of current year is: 1993
2 digit of current year is: 93In the above example,I used 5th February 1993 as date format. You may have different date format. Don’t worry. Any format will work as long as you follow the supported date and time formats
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-
format('Y'); echo "
"; echo "2 digit of current year is: " . $now->format('y'); ?>Output:
4 digit of current year is: 2014
2 digit of current year is: 14Method 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 –
format('Y'); echo "
"; echo "2 digit of current year is: " . $dateObj->format('y'); ?>Output:
4 digit of current year is: 1993
2 digit of current year is: 93