How to insert a clock on our website

How to insert a clock on our website 1

The Best WordPress Hosting

After many years of using and jumping from hosting to hosting, we have finally found the best one, and we want to share it with you.

Time is very important in the Internet world, especially when it comes to servers of any kind. For example, there are online games where some events depend on the server time. Today we will show you how to insert a practical clock on your website that displays the server time..

javascript Clock

The clock is set to javascript and works by capturing the hours, minutes and seconds every second in one variable each. This way only the ordered variables have to be displayed afterwards to achieve the effect of a clock.

<html>

<head>

Web clock displaying server time.

<script language=»JavaScript»>

function moveClock(){

currentMoment = new Date()

time = currentMoment.getHours()        //Captures the time in the variable "time".

minute = currentMinute.getMinutes()                             //Captures the minutes in the variable "minute".

second = currentMoment.getSeconds()          //Captures the seconds in the variable "second".

if (second < 10) {          //If the time is less than 10 add a 0 in front of it.

second = "0" + second;

}

if (minute < 10) {             //If the minutes are less than 10 add a 0 in front of them.

minute = "0" + minute;

}

if (time < 10) {                   //If the seconds are less than 10, add a 0 in front of them.

time = "0" + time;

}

timeImprintable = hour + " : " + minute + " : " + second                              //Sort the time and display as follows: 00:00:00.

document.form_clock.clock.value = printable_time      //Display the clock on the screen.

setTimeout("moveClock()",1000)         //Changes the value of the clock every second.

}

</script>

</head>

<body>

<form>

<input name=»reloj» size=»8″>

</form>

</body>

</html>

As you can see, the script is quite simple and can be very useful. To use it we only have to save it in a file hora.php and call it through the following PHP code:

<?php include(‘hora.php’); ?>

The final result will be as follows:

Final result of the web clock

Remember that you can ask any questions you may have through the forum and we will answer them without any problem.

3 thoughts on “Cómo insertar un reloj en nuestra web”

  1. David Gonzalez

    I just wanted to say that it doesn't show the server time, but the client time because the javascript code is executed by our browsers, to show the server time you would have to use some php function related to the time that I don't know yet.

Leave a Comment

Your email address will not be published.

en_GBEnglish
Scroll to Top