How To

How to Display Current Running Clock in PHP

To Display Current Running Clock in PHP, we will use a PHP script where we set the default time zone and time,...

Written by Bikash · 50 sec read >
How-to-Display-Current-Running-Clock-in-PHP

To Display Current Running Clock in PHP, we will use a PHP script where we set the default time zone and time, a jQuery code to take that response from the PHP script then show it to the web page.

How to Display Current Running Clock in PHP
Display Current Running Clock in PHP

You can say it will partially display running time or live clock using PHP. Here we will create 2 files, one is where we code HTML and JS then a PHP script to show time.

  • index.html
  • timeScript.php

PHP Program to Display Current Running Clock

index.html

timeScript.php

Output:

PHP Running Time Display

Code Highlights:

  • Create an HTML div element where we show the time.
  • Include a jQuery script that uses setInterval() to call the PHP script at a given time.
  • At last, we create a PHP file where we set the time zone and time using the PHP date function.

Here is a complete PHP program to display a digital clock that displays the current time of the server or set default time zone.

If you want to know more about the set interval function https://www.w3schools.com/jsref/met_win_setinterval.asp

Also Check:

Happy Coding..!

Was this article helpful?
YesNo
Written by Bikash
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP. Profile

7 Replies to “How to Display Current Running Clock in PHP”

  1. This is not good idea at all. Request to the server can take more than one second, so your clock can get stuck easily. Better way to go is get use of JavaScript’s own date functions – then you don’t need any request to a server. Something like:

    $(document).ready(function() {
    setInterval(function () {
    // get current time
    var dateObj = new Date();
    // write it to HTML
    $(‘#runningTime’).html(dateObj.toLocaleTimeString());
    // or use dateObj.getHours(), dateObj.getMinutes() and dateObj.getSeconds() to format it to your needs
    }, 1000); // get current time and write to HTML every 1000 ms (1 second)
    });

    1. You are right but if we use only JS to do this task then What will be the meaning of this article “How to Display Current Running Clock in PHP”.
      Our main purpose is to do this with the help of PHP not only JS.

      Thanks

  2. Hi , I have a problem . I followed the code given about . However , the clock was static and does not change time unless I will refresh it . What could be the possible problem for this ? Thanks in advance ❤

  3. I tried this in a and when it refreshes time my input from the form is deleted. How to avoid this?

Leave a Reply

Your email address will not be published. Required fields are marked *