11

How to auto update the copyright year in the footer section of the site in Magento 2.

enter image description here

0

6 Answers 6

22

One possible hack can help us to modify the year dynamically.

Go to -> Admin -> General, choose Design -> Expand the Footer section and paste the below code.

Copyright © <script>document.write(new Date().getFullYear())</script> Magento. All rights reserved.

Remove the cache and check.

4
  • Hi Thanks for your answer I'll try this one also Dec 26, 2016 at 10:01
  • This gets the job done but I do have to wonder if search engine site spiders will pick up the correct copyright year.
    – jschrab
    Jan 12, 2018 at 21:28
  • 4
    This doesn't work on 2.2.2 as it doesn't accept html elements. Aug 19, 2019 at 8:23
  • document.write(new Date().getFullYear()) is deprecated. Is there an PHP version of the same thing? May 7, 2020 at 19:04
11

Place the following contents in this file:

{theme_dir}/Magento_Theme/templates/html/copyright.phtml

<?php /* @escapeNotVerified */ echo preg_replace('/(^|\s)(\d{4})(\s|$)/m', " ".date('Y'). " ", $block->getCopyright()); ?>
1
  • 3
    I like this solution the best - it gives you control over the text, yet flexibility for the copyright year to change. What I do to extend this is <?= /* @escapeNotVerified */ str_ireplace('{{year}}', date('Y'), $block->getCopyright()) ?> ... and then use "{{year}}" copyright text in the footer admin. That way I can have full control over the text along with the auto-updating year.
    – jschrab
    Jan 12, 2018 at 21:27
7

Place the following contents in this file: {theme_dir}/Magento_Theme/templates/html/copyright.phtml

<small class="copyright">
    <span>Copyright &copy; You <?php echo date('Y') ?>, All Rights Reserved.</span>
</small>

Then flush the cache.

1
  • Hello Thanks for this answor I'll try this one.Thanks Aaron :) Dec 26, 2016 at 10:11
3

This how I would do it. overwrite copyright.phtml:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<small class="copyright">
    <span><?= /* @escapeNotVerified */ str_replace ( '{{year}}', date('Y'), $block->getCopyright()) ?></span>
</small>

Then go to Content->Design->Configuration Choose a theme Edit->footer->copyright add this:

Copyright © {{year}} Magento. All rights reserved.

Done!

1
  • this helped me. May 7, 2020 at 19:39
1

The best way to do this would be by creating an after plugin on the getCopyright method in Magento\Theme\Block\Html\Footer. It is not good practice to add logic in a template.

Add the following in a custom module in the etc/frontend/di.xml file

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Theme\Block\Html\Footer">
        <plugin name="Vendor_Module::UpdateCopyrightWithCurrentYear" type="Vendor\Module\Plugin\Theme\Block\Html\Footer\UpdateCopyrightWithCurrentYear" />
    </type>
</config>

create Plugin/Theme/Block/Html/Footer/UpdateCopyrightWithCurrentYear.php within you module:

<?php
namespace Vendor\Module\Plugin\Theme\Block\Html\Footer;

use Magento\Theme\Block\Html\Footer;

class UpdateCopyrightWithCurrentYear
{
    /**
     * @param Footer $subject
     * @param string $result
     * @return string $result
     */
    public function afterGetCopyright(Footer $subject, $result)
    {
        $result = preg_replace_callback(
            '/(^|\s)(\d{4})(\s|$)/m',
            function($matches) {
                return $matches[2] != date('Y')?$matches[1] . $matches[2].' - '.date('Y') . $matches[3]:$matches[0];
            },
            $result);
        return $result;
    }
}

I borrowed Krishna ijjada's regex to match the year. Also this adds the current year in the copyright message so that the year when copyright started also stays visible.

0

It is necessary to think of the timezone, here is my answer ( {theme_dir}/Magento_Theme/templates/html/copyright.phtml ):

<?php
/* @var $block \Magento\Theme\Block\Html\Footer */

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

$year = ObjectManager::getInstance()->get( TimezoneInterface::class )->date()->format( 'Y' );
?>
<small class="copyright">
    <span><?= /* @escapeNotVerified */ $block->escapeHtml( __( 'Copyright &copy; %1 xxx.', $year ) ) ?></span>
</small>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.