Advertisement:
Read Later
In this tutorial, I will show you how to obtain time in different supported timezones using the time function in PHP. Besides, you will be able to display and update the time every second using a tiny JavaScript code on your server.
Below is a complete list of the timezones supported by PHP, which are useful with several PHP date functions(*).
Create a div element in HTML, having 'time' as the id parameter.
In PHP, using the date_default_timezone_set() function, define the timezone in which the time will be evaluated.
To get time as hour:minute:seconds in the 24/7 format, use the date("H:i:s", time()) function as shown below.
Save the PHP code in a file named time.php and load the time.php file every second in the mentioned div element by using setInterval and the load() function in JavaScript(jQuery).
------------HTML(div)--------------
<div id="time" style="background-color:#002699;background:linear-gradient(45deg, #002699, #1a53ff);width:320px;height:180px;margin:auto;display:block;border-radius:20px;text-align:center;"></div>
------------PHP(time.php)--------------
<?php
date_default_timezone_set('America/Chicago');
$America_Chicago = date("H:i:s", time());
echo "<p>America/Chicago: ".$America_Chicago."</p>";
date_default_timezone_set('Antarctica/South_Pole');
$Antarctica_South_Pole = date("H:i:s", time());
echo "<p>Antarctica/South_Pole: ".$Antarctica_South_Pole."</p>";
date_default_timezone_set('Asia/Bangkok');
$Asia_Bangkok = date("H:i:s", time());
echo "<p>Asia/Bangkok: ".$Asia_Bangkok."</p>";
date_default_timezone_set('Europe/London');
$Europe_London = date("H:i:s", time());
echo "<p>Europe/London: ".$Europe_London."</p>";
?>
------------JavaScript(jQuery)--------------
setInterval(() => {
$("#time").load('time.php');
}, 1000);
Result:
(*) https://www.w3schools.com/php/php_ref_timezones.asp