How to automatically update copyright year on website ?

Best Division

Best Division  @bestdivision

Published On - Last Updated -

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).


<!DOCTYPE html>
    <!-- Webiste font-size changes when screen size changes. -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Updating Copyright Year Automatically Using HTML and PHP</title>
    <div class="footer-copyright text-center py-3">&copy; <?php echo date("Y"); ?> Copyright:
        <a href="#"></a>

In Javascript

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

phpCopy code

<p>&copy; <span id="year"></span></p>
  1. Add the following JavaScript code to the same HTML file, before the closing **</body>** tag:
  document.getElementById("year").innerHTML = new Date().getFullYear();

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:
<p>&copy; <%= year %></p>

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

app.get('/', (req, res) => {
  res.render('copyright', { year: new Date().getFullYear() });

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:
<% include copyright %>

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.

Similar Blogs
0 Commentssort Sort By

@abcd 1 days ago

Aquí los que apoyamos a Los del limit desde sus inicios..