Question

How to Get the Current Year in PHP for Dynamic Copyright Footers

phpdate

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>&copy; <?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.

Take Quiz

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>&copy; <?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:

<?php
$startYear = 2021;
$currentYear = date('Y');
?>

<footer>
    <p>&copy; <?php echo $startYear; ?><?php if ($startYear != $currentYear) echo '–' . $currentYear; ?> My Website</p>
</footer>

This shows:

  • © 2021 My Website if the current year is 2021
  • © 2021–2026 My Website if the current year is later

Why 'Y' and not 'y'?

  • 'Y' gives a 4-digit year: 2026
  • 'y' gives a 2-digit year: 26

For copyright notices, 'Y' is usually the correct choice.

Step by Step Execution

Consider this code:

<?php
$currentYear = date('Y');
echo "© $currentYear My Website";

Here is what happens step by step:

  1. PHP reaches date('Y').
  2. It checks the server’s current date and time.
  3. The format 'Y' tells PHP to extract the full year.
  4. PHP returns a string such as '2026'.
  5. That value is stored in $currentYear.
  6. echo outputs 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:

2026

And the final output becomes:

© 2026 My Website

Real World Use Cases

Getting the current year in PHP is useful in many places beyond a footer.

Website footer

<p>&copy; <?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.

$currentYear = date('Y');
// Use $currentYear in reporting logic

These examples show that the same simple function is often part of larger app features.

Take Quiz

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>&copy; <?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 copyrightYear($startYear) {
    $currentYear = date('Y');
    return $startYear == $currentYear ? $currentYear : $startYear . '–' . $currentYear;
}

echo '© ' . copyrightYear(2021) . ' My Company';

This is common when the same formatting rule is reused across templates.

Early simplification

Developers often choose the simplest working option first:

<?php echo date('Y'); ?>

Then they refactor into variables or helpers only if the project grows.

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>&copy; date('Y') My Website</p>

This prints the text literally instead of running PHP.

Use:

<p>&copy; <?php echo date('Y'); ?> My Website</p>

4. Hardcoding the year

Broken example:

<p>&copy; 2023 My Website</p>

This will become outdated.

Use dynamic output instead:

<p>&copy; <?php echo date('Y'); ?> My Website</p>

5. Ignoring timezone settings in larger apps

If your app depends heavily on date logic, an incorrect timezone can cause wrong results near midnight or year boundaries.

Set it explicitly when needed:

date_default_timezone_set('UTC');

Comparisons

OptionExampleResultBest use
Hardcoded year2026Fixed valueAlmost never for footers
date('Y')date('Y')4-digit yearBest choice for copyright notices
date('y')date('y')2-digit yearRarely useful for this case
Store in variable$year = date('Y');Reusable valueGood when used multiple times
Inline output<?php echo date('Y'); ?>Direct outputGood for simple templates

date('Y') vs DateTime

ApproachExampleWhen to use
date('Y')date('Y')Simple current year output
DateTime(new DateTime())->format('Y')More complex date handling

For a footer, date('Y') is usually simpler and clearer.

Take Quiz

Cheat Sheet

// Current 4-digit year
$date = date('Y');

// Output directly
echo date('Y');

// In HTML
<p>&copy; <?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 echo if 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>&copy; <?php echo date('Y'); ?> Company Name</p>
</footer>

Year range pattern

<?php
$startYear = 2021;
$currentYear = date('Y');
echo $startYear == $currentYear ? $currentYear : $startYear . '–' . $currentYear;

Edge case

  • Around New Year, timezone settings can affect which year PHP sees on the server.

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.

Is date() enough, or should I use DateTime?

For a simple footer year, date('Y') is enough. Use DateTime when you need more advanced date handling.

Take Quiz

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.
Take Quiz

Keep learning

Related questions

View more