Sign in QQ login WeChat login
All categories

PHP+js+html code to obtain the current Beijing time and local time

finchui fifty-six 2024-05-11 22:41:58

Php code

You need to create a separate php file, for example, get_beijing_time.php

 <? php //Send a request to obtain the Beijing timestamp $response = file_get_contents(' http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp '); echo $response;


Html code

 <h1>Beijing Time:</h1> <div id="beijingTime"></div> <h1>Local time:</h1> <div id="localTime"></div>


Js code

You need to rely on jquery

  function getBeijingTime() { //Send a request to obtain the Beijing timestamp             $.ajax({               url: 'get_beijing_time.php',               dataType: 'json',               success: function(data) { //Check whether the timestamp is obtained successfully                 if (data && data.data && data.data.t) {                   var timestamp = Math.floor(data.data.t / 1000); //Remove the millisecond part                   var beijingTime = new Date(timestamp * 1000); //Convert to JavaScript time object //Format Year Month Day Hour Minute Second                   var year = beijingTime.getFullYear();                   var month = ("0" + (beijingTime.getMonth() + 1)).slice(-2);                   var day = ("0" + beijingTime.getDate()).slice(-2);                   var hours = ("0" + beijingTime.getHours()).slice(-2);                   var minutes = ("0" + beijingTime.getMinutes()).slice(-2);                   var seconds = ("0" + beijingTime.getSeconds()).slice(-2); //Display the hour, minute and second of the year, month and day on the page                   $('#beijingTime').text(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);                 } else {                   console.error('Failed to fetch Beijing time.');                 }               },               error: function(xhr, status, error) {                 console.error('Error fetching Beijing time:', error);               }             });           }           function updateLocalTime() { //Get local time             var localTime = new Date(); //Format Year Month Day Hour Minute Second             var year = localTime.getFullYear();             var month = ("0" + (localTime.getMonth() + 1)).slice(-2);             var day = ("0" + localTime.getDate()).slice(-2);             var hours = ("0" + localTime.getHours()).slice(-2);             var minutes = ("0" + localTime.getMinutes()).slice(-2);             var seconds = ("0" + localTime.getSeconds()).slice(-2); //Display the hour, minute and second of the year, month and day on the page             $('#localTime').text(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);           } //Update Beijing time when the page is loaded           getBeijingTime(); //Update Beijing Time once per second           setInterval(getBeijingTime, 1000); //Update local time once per second           setInterval(updateLocalTime, 1000);


  • Dislike( one

Link to this article: https://www.finchui.com/web/108.html

Guess you like it

Netizen comments

Hot commodity
Popular articles
Hot tags
Related labels
 FinchUI Store Edition

share

Copy Link

Xinglan Studio Online consultation

Working hours: 9:00-22:00
Saturday and Sunday: 14:00-22:00