How To Get Current Year In PHP

mins

The Date is an inbuilt function used to format the timestamp. The computer stores date and time in UNIX Timestamp. This time is measured as a number of seconds since Jan 1, 1970. Since this is impractical for humans to read, PHP converts timestamp to a format that is readable and more understandable to humans.

Syntax:

date(format, timestamp)

Explanation:

  • The format parameter in the date() function specifies the format of returned date and time.
  • The timestamp is an optional parameter. If it is not included then the current date and time will be used.
  • In order to get the current year as four digits, Y is used as a parameter to date() function as shown in the below:
<?php 
// PHP program to get the current year

// Store the year to the variable
$year = date("Y"); 

// Display the year
echo $year;
?> 

Output: 2021

Format options available in date() function

The format parameter of the date() function is a string that can contain multiple characters allowing to generate dates in various formats are listed below:

  • d – Represents the day of the month. It uses two digits with leading zeros (01 or 31).
  • D – Represents day of the week in the text (Mon to Sun).
  • m – Represents month in numbers with leading zeros (01 or 12).
  • M – Represents month in text, abbreviated (Jan to Dec).
  • y – Represents year in two digits (08 or 14).
  • Y – Represents year in four digits (2008 or 2014).

This is the JS alternative

This is handy for static websites that don't render server-side.

<script>
// JS to get the current year

var CurrentYear = new Date().getFullYear()

// write current year

document.write(CurrentYear)

</script>

Output:

I do not use cookies on this website

🍪🍪 What cookies?


Your privacy is important. That's why this website doesn't track visitors with fingerprinting, your IP address or cookies.


Web analytics data is your own. Reclaim it.

Gain back control with a privacy-focused web analytics platform (using a cookie-less architecture) from: Data Centurion.