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 like10-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
Format | Description | Example returned values |
---|---|---|
Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
y | A two-digit representation of a year | Examples: 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 © <?php echo date("Y"); ?>
Show Start and Current Year
Copyright © 2017-<?php echo date("Y"); ?>
Additionally, read our guide:
- How to Select Data Between Two Dates in MySQL
- Error After php artisan config:cache In Laravel
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- How to Convert Base64 to Image in PHP
- Check If A String Contains A Specific Word In PHP
- Dynamically Populate A Select Field’s Choices In ACF
- How To Find Duplicate Records in Database
- 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!