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.

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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<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
1 2 3 4 |
<?php date_default_timezone_set('Asia/Kolkata'); echo $runningTime = date('h:i:s'); ?> |
Output:

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:
- How JavaScript For Loop Works (Complete Guide With Example)
- Create Repository & Push Code To GitHub First Time
- Migrate WordPress Site To New Host Manually
- Set-up Firebase Project using Firebase Console
Happy Coding..!
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)
});
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
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 ❤
Same issue is happening with me
I tried this in a and when it refreshes time my input from the form is deleted. How to avoid this?