PHP + js + html implementação de código para obter o horário de Pequim agora e o tempo local

php代码

需要创建一个独立的php文件,文件名称例如:get_beijing_time.php

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


html代码

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


Código js

需要依赖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);


Rótulos:北京时间
Não gosto.1

Link para este artigo:https://pt.finchui.com/web/108.html

Comentários dos internautas

Eu acho que você gosta.

Mercadorias populares
Artigos populares
Rótulos quentes
Rótulos relevantes
Edição da FinchUI Store Região Especial Preferencial de Hong Kong

Compartilhamento

Copia do link

Estúdio Lan XingAdicione um amigo

Horário de trabalho: 9: 00 - 22: 00
Sábado, domingo: 14: 00 - 22: 00
wechat
Digitalizar código para adicionar WeChat de atendimento ao cliente