php + js + html 구현 현재 베이징 시간과 현지 시간을 얻는 코드

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>


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);


싫다1

이 문서의 링크:https://ko.finchui.com/web/108.html

네티즌 댓글

아마 좋아하실 겁니다.

인기 상품
인기 기사
핫 레이블
관련 레이블
Finch UI 스토어 버전

공유

링크 복사
출 근 시간 : 9: 00 ~ 22: 00
토요일, 일요일: 14: 00 - 22: 00
wechat
코드를 스캔하여 고객서비스 위챗을 추가하다