PHP + js + html Implementazione del codice per ottenere ora ora di Pechino e ora locale

Codice PHP

È necessario creare un file php separato, ad esempio: get_beijing_time.php

<?php
// 发送请求获取北京时间戳
$response = file_get_contents('http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp');
echo $response;


Il codice HTML

<h1>北京时间:</h1>
<div id="beijingTime"></div>
<h1>本地时间:</h1>
<div id="localTime"></div>


Il codice JS

Dipende da jquery

 function getBeijingTime() {
            // 发送请求获取北京时间戳
            $.ajax({
              url: 'get_beijing_time.php',
              dataType: 'json',
              success: function(data) {
                // 检查是否成功获取时间戳
                if (data && data.data && data.data.t) {
                  var timestamp = Math.floor(data.data.t / 1000); // 去掉毫秒部分
                  var beijingTime = new Date(timestamp * 1000); // 转换成 JavaScript 时间对象
                  // 格式化年月日时分秒
                  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);
                  // 将年月日时分秒显示在页面上
                  $('#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() {
            // 获取本地时间
            var localTime = new Date();
            // 格式化年月日时分秒
            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);
            // 将年月日时分秒显示在页面上
            $('#localTime').text(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);
          }
          // 页面加载时更新北京时间
          getBeijingTime();
          // 每秒更新一次北京时间
          setInterval(getBeijingTime, 1000);
          // 每秒更新一次本地时间
          setInterval(updateLocalTime, 1000);


Non piace1

Questo articolo è collegato:https://it.finchui.com/web/108.html

Commento degli utenti di rete

Immagino che ti piaccia.

Merce caldo
Articoli caldi
Etichette calde
Etichette relative
Edizione del negozio FinchUI Zona privilegiata di Hong Kong

Condivisione

Copiare il link

Lo studio delle stelle.Aggiungi amici

Orario di lavoro: 9: 00 - 22: 00
Sabato, Domenica: 14: 00 - 22: 00
wechat
Aggiungi il servizio micro