PHP+js+html code to obtain the current Beijing time and local time
Php code
<? 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
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);
-
Like( eleven ) -
Dislike( one )