时区PHP刷新

时间:2015-04-20 17:21:04

标签: javascript php time

我需要每隔一秒刷新一次我的时区。

我有这段代码:

$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London')); echo
$now->format("H:i:s A");

我想在我的服务器上按时区(状态)显示时间,并刷新。

喜欢这段代码,但我的时区状态如欧洲/伦敦或其他州:

<script type="text/javascript"> 
function display_c(){
    var refresh=1000; // Refresh rate in milli seconds
    mytime=setTimeout('display_ct()',refresh)
}

function display_ct() {
    var strcount
    var x = new Date()
    document.getElementById('ct').innerHTML = x;
    tt=display_c();
}
</script>


<body onload=display_ct();>
<span id='ct' ></span>

那么JavaScript的代码显示DateTimeZone('America/New_York')是否有刷新时间?

示例:http://www.thetimenow.com/canada

我发现这个var java脚本但是它的错误可以任何一个plz corecet这个代码

    <script>
var server_name = "www.site.com";
var time_format = '24';

var timezones = ["America/Montreal","Etc/GMT+5","Etc/GMT+4","Etc/GMT+6","Etc/GMT+5","Etc/GMT+8","Etc/GMT+7","GMT","UTC"];
document.writeln(" $date = new DateTime($ref['lastupdated']); "); 
document.writeln(" $date->setTimezone(new DateTimeZone('America/Los_Angeles')); "); 

var current_time = {"America/Montreal":["<? print date("H", time())?>","<? print date("i", time())?>","<? print date("s", time())?>","<? print date("l", time())?>","<? print date("F", time())?>","<? print date("d", time())?>","<? print date("Y", time())?>"]]};


</script

真正的代码php是

<?Php

$date = new DateTime($ref['lastupdated']);
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $date->format('m/d/y g:i:s a');
?>

更新php中的源代码

<?php
echo "<div id=\"output\"></div>";
echo "<script type='text/javascript'>
( function()
{ 
// use JavaScript Date to display current date
// in a div (#displayJsDate)
var output = document.getElementById('output');


var start = moment("<? print date("c")?>");
var delta = start - moment();

setInterval(function(){
    var now = moment().add(delta,'ms');
    var m = now.tz('$data[zone]');    // Set the time zone as desired
    var s = m.format('YYYY-MM-DD h:mm:ss A');
    output.innerHTML = s;
}, 1000);
})();
</script>";



?>

2 个答案:

答案 0 :(得分:0)

您可以使用setTimeout()创建递归javascript函数以不断更新当前时间。见this example

答案 1 :(得分:0)

这与PHP无关。您需要一个能够理解时区的JavaScript库。

例如,使用moment-timezone,您可以执行以下操作:

&#13;
&#13;
var output = document.getElementById("output");

var start = moment("2015-01-01T00:00:00Z"); // Current time UTC value from server
var delta = start - moment();

setInterval(function(){
    var now = moment().add(delta,'ms');
    var m = now.tz("America/New_York");    // Set the time zone as desired
    var s = m.format("YYYY-MM-DD h:mm:ss A");
    output.innerHTML = s;
}, 1000);
&#13;
<script src="//momentjs.com/downloads/moment.min.js"></script>
<script src="//momentjs.com/downloads/moment-timezone-with-data-2010-2020.min.js"></script>
<div id="output"></div>
&#13;
&#13;
&#13;

jsFiddle