BestDivision Logo

How to automatically update copyright year on website ?

BestDivision
BestDivision  @bestdivision
Created At - 2020-05-24
Last Updated - 2024-08-28

Table of Contents

  • How to update copyright year automatically each year?
      • In PHP
      • In Javascript
      • In NodeJs

How to update copyright year automatically each year?

No body wants to update each year their websites copyright manually. And most of beginner runs away from JavaScript. Fortunately, JavaScript isn't required for this task.(Though we can do it using JavaScript too).

In PHP

In Javascript

  1. Add the following HTML code to your website where you want the copyright year to appear:

phpCopy code

  1. Add the following JavaScript code to the same HTML file, before the closing </body> tag:

This code uses the new Date() constructor to create a new Date object, which represents the current date and time. The getFullYear() method is then used to extract the current year from the date object. Finally, the value of the year is inserted into the HTML element with the ID of "year" using the innerHTML property.

Now, whenever your website is loaded, the current year will be automatically inserted into the HTML element, updating the copyright year on your website.

You can also add this JavaScript code to an external .js file and include it in your HTML file to keep your code organized and reusable.

In NodeJs

  1. Create a new file called copyright.ejs in your views directory and add the following HTML code:

This code uses Embedded JavaScript (EJS) to insert the value of the year variable into the HTML.

  1. In your Node.js application, add the following code to render the copyright.ejs file and pass the current year to it:

javascriptCopy code

This code creates a new GET route for the root URL of your application. When the route is accessed, the render method is used to render the copyright.ejs file and pass the current year to it as a local variable.

  1. In your main layout file, include the rendered copyright.ejs file using the following code:

Now, whenever your Node.js application is loaded, the current year will be automatically inserted into the HTML element, updating the copyright year on your website.

Share

‌

  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌