How to Display Current Running Clock in PHP

To Display Current Running Clock in PHP, we 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 partially display running time or live clock use PHP. Here we 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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="runningTime"></div>

<script type="text/javascript">
$(document).ready(function() {
 setInterval(runningTime, 1000);
});
function runningTime() {
  $.ajax({
    url: 'timeScript.php',
    success: function(data) {
       $('#runningTime').html(data);
     },
  });
}
</script>

timeScript.php

<?php
  date_default_timezone_set('Asia/Kolkata');
  echo $runningTime = date('h:i:s');
?>

Output:

PHP Running Time Display

Code Highlights:

  • Create an HTML div element where we show the time.
  • Include a jQuery script which and use setInterval() to call PHP script at a given time.
  • At last, we create a PHP file where we set time zone and time using 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 wan to know more about set interval function https://www.w3schools.com/jsref/met_win_setinterval.asp

Also Check:

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.
Posts created 163

3 thoughts on “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

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
mariobet - supertotobet - escort -

takip7.com

We are now accepting Guest Posting and creation of Backlinks
This is default text for notification bar