Home » How To Get The Current Year In PHP

How To Get The Current Year In PHP

Last updated on December 30, 2020 by

Table of Contents
1. Get Current Year Using date() Function
2. Using the DateTime Class’s Object
3. Real-Life Examples

In this article, we will teach you how to get the current year in PHP with some good examples. We hope you like it so are you ready to do that? Let’s just jump into it.

01 Get Current Year Using date() Function

PHP provides an inbuilt date() function by using that we can easily get the current year.

Syntax:

date( format, timestamp )
  • The format parameter is required and used to return in which format you want to show date or time for example, dd-mm-yy which will then return the date like 10-05-20
  • The timestamp parameter is optional. This parameter is used to convert a given date or time into the desired format. If you don’t feed the date or time into this parameter, then the date() function will use the current date and time.

Use the following formats to get your desired year

FormatDescriptionExample returned values
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two-digit representation of a yearExamples: 99 or 03
<?php  

/*@ Get the current year */
  
echo "Current Year with 4 digits is : ".date("Y");  

echo "Current Year with 2 digits is :".date("y");  

Output:

Current Year with 4 digits is : 2020 

Current Year with 2 digits is : 20

02 Using the DateTime Class’s Object

PHP provides an inbuilt DateTime Class by using that you can easily format your dates. This will be more useful when it comes to use a more object-orientated approach.

$currentDate = new DateTime();  // As a default, it will take current timestamp

$getYear = $currentDate->format("Y");

echo $getYear;

On the above example, we created a DateTime object and then retrieved a year by just using format() function on an object by passing the format string “Y” inside it.

If you don’t pass any parameters inside DateTime then it will take the current timestamp as a default.

Use lowercase “y” format string to get the 2-digit year. Let’s now extract the year from the given date or timestamp by using the DateTime Class.

$currentDate = new DateTime('2020-06-01 00:00:00');

$getYear = $currentDate->format("Y");

echo $getYear; // 2020

03 Real Life Examples

Dynamic Copyright Year in PHP

Show Only Current Year
Copyright &copy; <?php echo date("Y"); ?>
Show Start and Current Year
Copyright &copy; 2017-<?php echo date("Y"); ?>

Additionally, read our guide:

  1. How to Select Data Between Two Dates in MySQL
  2. Error After php artisan config:cache In Laravel
  3. Specified Key Was Too Long Error In Laravel
  4. AJAX PHP Post Request With Example
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Calculate Age From Birthdate
  11. How to Convert Base64 to Image in PHP
  12. Check If A String Contains A Specific Word In PHP
  13. Dynamically Populate A Select Field’s Choices In ACF
  14. How To Find Duplicate Records in Database
  15. How To Create Dynamic Variable In PHP

That’s it for now. We hope this article helped you to get the current year in PHP.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!

 
 

Leave a Comment