Question
I want to display a copyright notice in the footer of a website, but I do not want the year to become outdated.
How can I use PHP to get the current year automatically so the footer always stays up to date?
Short Answer
By the end of this page, you will understand how to get the current year in PHP, display it in a footer, and use it safely in real projects. You will also learn the date() function, how formatting works, and a few common mistakes to avoid.
Concept
In PHP, you can get the current year by using the built-in date() function:
$date = date('Y');
The format string 'Y' tells PHP to return the year as a 4-digit number, such as 2026.
This matters because many parts of a website or application need date-based values:
- copyright footers
- timestamps
- reports
- logs
- invoices
- archive pages
Using PHP to generate the year automatically means you do not need to manually update your site every January. That reduces maintenance work and helps keep the site accurate.
A common real use case is a footer like this:
<p>© <?php echo date('Y'); ?> My Website</p>
Each time the page is rendered, PHP calculates the current year on the server and outputs it into the HTML.
If your application depends on timezone-sensitive dates, PHP uses the server timezone unless you set one explicitly. For a year display in a footer, this usually works fine, but in production applications it is still a good idea to configure the timezone properly.
Mental Model
Think of date() like a clock and calendar attached to your server.
You ask PHP a question such as:
- “What year is it?” →
date('Y') - “What month is it?” →
date('m') - “What day is it?” →
date('d')
The format characters are like instructions. The 'Y' instruction means: “Give me the full year.”
So instead of typing 2026 yourself and having to remember to change it later, you let PHP check the server’s calendar every time the page loads.
Syntax and Examples
The basic syntax is:
date('Y')
Example: store the year in a variable
<?php
$currentYear = date('Y');
echo $currentYear;
This prints something like:
2026
Example: use it directly in HTML
<footer>
<p>© <?php echo date('Y'); ?> My Website</p>
</footer>
This might render as:
<footer>
<p>© 2026 My Website</p>
</footer>
Example: year range for older websites
If a site started in 2021, you may want to show a range:
= ;
= ();
<footer>
<p>© ; ( != ) . ; My Website</p>
</footer>
Step by Step Execution
Consider this code:
<?php
$currentYear = date('Y');
echo "© $currentYear My Website";
Here is what happens step by step:
- PHP reaches
date('Y'). - It checks the server’s current date and time.
- The format
'Y'tells PHP to extract the full year. - PHP returns a string such as
'2026'. - That value is stored in
$currentYear. echooutputs the final text.
So the browser receives something like:
© 2026 My Website
Trace example
If the current date on the server is:
2026-05-04 10:30:00
Then:
date('Y')
returns:
Real World Use Cases
Getting the current year in PHP is useful in many places beyond a footer.
Website footer
<p>© <?php echo date('Y'); ?> Example Company</p>
Generating file names
$filename = 'report-' . date('Y') . '.pdf';
Example output:
report-2026.pdf
Logs or archive folders
$archiveFolder = '/archives/' . date('Y');
Showing current year in a dashboard
echo 'Statistics for ' . date('Y');
Filtering records by year
A PHP app may use the current year when building a database query or report period.
Real Codebase Usage
In real projects, developers usually do more than just call date('Y') inline everywhere.
Common pattern: assign once, reuse
<?php
$currentYear = date('Y');
This is useful when the same value appears in multiple places.
Configuration and layout templates
In many PHP applications, the footer is stored in a shared layout file:
<footer>
<p>© <?php echo date('Y'); ?> My Company</p>
</footer>
This keeps the logic in one place.
Guarding for date consistency
In larger systems, timezone is often configured centrally:
date_default_timezone_set('UTC');
This avoids surprises when different servers use different timezone settings.
Helper function pattern
Some codebases wrap formatting logic in a helper:
<?php
function () {
= ();
== ? : . . ;
}
. () . ;
Common Mistakes
1. Using the wrong format character
Broken example:
echo date('y');
This outputs a 2-digit year like:
26
Use this instead:
echo date('Y');
2. Forgetting to output the value
Broken example:
<?php date('Y'); ?>
This calculates the year but does not display it.
Use:
<?php echo date('Y'); ?>
3. Mixing PHP into HTML incorrectly
Broken example:
<p>© date('Y') My Website</p>
This prints the text literally instead of running PHP.
Comparisons
| Option | Example | Result | Best use |
|---|---|---|---|
| Hardcoded year | 2026 | Fixed value | Almost never for footers |
date('Y') | date('Y') | 4-digit year | Best choice for copyright notices |
date('y') | date('y') | 2-digit year | Rarely useful for this case |
| Store in variable | $year = date('Y'); | Reusable value | Good when used multiple times |
| Inline output |
Cheat Sheet
// Current 4-digit year
$date = date('Y');
// Output directly
echo date('Y');
// In HTML
<p>© <?php echo date('Y'); ?> My Website</p>
// 2-digit year (usually not what you want)
echo date('y');
Quick rules
- Use
date('Y')for a 4-digit year. - Use
echoif you want to display it. - Use
'Y', not'y', for copyright notices. - PHP uses the server timezone unless you set one.
Common footer pattern
<footer>
<p>© <?php echo date('Y'); ?> Company Name</p>
</footer>
Year range pattern
= ;
= ();
== ? : . . ;
FAQ
How do I print the current year in PHP?
Use:
echo date('Y');
What does date('Y') mean in PHP?
It tells PHP to return the current year as a 4-digit value, such as 2026.
Should I use Y or y in PHP for a copyright year?
Use Y. It gives the full year. y only gives the last two digits.
Why is my PHP year not showing on the page?
You may have forgotten echo, or the file may not be running as PHP.
Can I use PHP to show a copyright year range?
Yes. You can combine a start year with the current year.
$startYear = 2021;
$currentYear = date('Y');
Does timezone matter when getting the current year in PHP?
Usually not for simple footers, but it can matter near midnight or New Year. Set the timezone if your application needs consistency.
Mini Project
Description
Build a reusable website footer that automatically displays the current copyright year. This project demonstrates how PHP can generate dynamic content inside HTML and helps you avoid hardcoded dates that become outdated.
Goal
Create a PHP footer that always shows the correct current year and optionally supports a year range.
Requirements
- Display the current year automatically using PHP.
- Show the company or website name in the footer.
- Support a start year so the footer can display either a single year or a year range.
- Keep the code readable and easy to reuse in a template file.
Keep learning
Related questions
Converting HTML and CSS to PDF in PHP: Core Concepts, Limits, and Practical Approaches
Learn how HTML-to-PDF conversion works in PHP, why CSS support varies, and how to choose practical approaches for reliable PDF output.
How PHP foreach Actually Works with Arrays
Learn how PHP foreach works internally, including array copies, internal pointers, by-value vs by-reference behavior, and common pitfalls.
How to Check String Prefixes and Suffixes in PHP
Learn how to check whether a string starts or ends with specific text in PHP using simple functions and practical examples.