How do I use PHP to get the current year?

Getting the current year in PHP is a common task, especially when dealing with dynamic content or generating reports. Thankfully, PHP provides a simple way to obtain the current year using the powerful date() function.

Solution

// Get the current year
$currentYear = date('Y');

// Output the current year
echo "The current year is: $currentYear";

// Outputs
The current year is: 2024

In our above example, we provide the PHP date function the format of which we'd like our date displayed. In this case, we want to output the year, which is a capital "Y".

You can use this method to dynamically display the current year on your website or in your PHP applications. This is similar to the NOW() function in MySQL, which we can also achieve using PHP. As PHP doesn't have that function we can use a similar function to achieve the same output as MySQL's NOW().

This approach is particularly useful for copyright notices, dynamic footers, or any other places where you want to automatically display the current year. For example, in a website footer. There are also other ways to display dates in PHP including how to convert a date.

Senior PHP developer with near two decades of PHP experience. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Related Dev Guides

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all