Write a php program to display a digital clock which displays the current time of the server.

7. WRITE A PHP PROGRAM TO DISPLAY A DIGITAL CLOCK WHICH DISPLAYS THE CURRENT TIME OF THE SERVER.

 SOLUTION  1  &  SOLUTION  2 

1. If xampp is in the system, save the program in C:\xampp\htdocs (start apache in xampp control panel)

2. If no xampp in the system, save the program in C:\Program Files\Apache Group\Apache2\htdocs   & Open Google Chrome & type http://localhost/prog7.php

prog7.php







STEPS :( click on image to zoom )

Write a php program to display a digital clock which displays the current time of the server.



OUTPUT :( click on image to zoom )

Write a php program to display a digital clock which displays the current time of the server.

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.

Write a php program to display a digital clock which displays the current time of the server.
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

In This Article

  • 1 PHP Program to Display Current Running Clock
  • 2 index.html
  • 3 timeScript.php
    • 3.1 Related

index.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

});

functionrunningTime(){

  $.ajax({

    url: 'timeScript.php',

    success:function(data){

       $('#runningTime').html(data);

     },

  });

}

timeScript.php

  date_default_timezone_set('Asia/Kolkata');

  echo$runningTime=date('h:i:s');

?>

Output:

Write a php program to display a digital clock which displays the current time of the server.
PHP Running Time Display

Code Highlights:

  • Create an HTML divelement 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:

  • 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..!

Was this article helpful?

YesNo

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.

How to display Current running clock in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How do I add a digital clock in HTML?

HTML Code: In this section, we have a dummy time in the format of “HH:MM:SS” wrapped inside a “div” tag..
Step 1: Create a function “showTime”..
Step 2: Create an instance of the Date object..
Step 3: Using the methods of Date object get “hours”, “minute” and “seconds”..
Step 4: Set AM/PM depending on the hour value..

How do I run a PHP program?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

How do I create a running clock in Javascript?

Javascript Clock Code (12 hours): function currentTime() { let date = new Date(); let hh = date. getHours(); let mm = date. getMinutes(); let ss = date. getSeconds(); let session = "AM"; if(hh == 0){ hh = 12; } if(hh > 12){ hh = hh - 12; session = "PM"; } hh = (hh < 10) ?