js getTimezoneOffset() in php


new Date().getTimezoneOffset()*(-1);

这个JS函数以分钟为单位返回机器的UTC时区。有什么PHP函数做同样的事情吗?
谢谢你. .

<?php // RAY_easy_client_time.php
error_reporting(E_ALL);

// USE JAVASCRIPT TO GET THE CLIENT TIME AND COMPUTE THE OFFSET FROM THE SERVER TIME

// LOCATION OF THE SERVER - COULD BE ANYWHERE
date_default_timezone_set('America/Denver');
// DIFFERENCE OF SERVER TIME FROM UTC
$server_offset_seconds = date('Z');
// WHEN THE FORM IS SUBMITTED
if (!empty($_POST))
{
    // JAVASCRIPT TELLS US THE CLIENT TIME OFFSET FROM GMT / UTC
    $client_offset_minutes = $_POST["date_O"];
    $client_offset_seconds = $client_offset_minutes * 60;
    // THE TIME WE WANT AT THE CLIENT LOCATION
    $client_timestring = 'TODAY 7:00AM';
    // MAKE THE COMPUTATIONS, INCORPORATING THE OFFSET FROM GMT
    $client_timestamp  = strtotime($client_timestring) + $client_offset_seconds;
    $server_timestamp  = $client_timestamp + $server_offset_seconds;
    $server_timestring = date('l, F j, Y at g:i a', $server_timestamp);
    echo "<br/>ACCORDING TO THE VALUE FROM PHP date Z";
    echo "<br/>SERVER IS LOCATED $server_offset_seconds SECONDS FROM UTC";
    echo "<br/>";
    echo "<br/>ACCORDING TO THE VALUE FROM JS dateObject.getTimezoneOffset()";
    echo "<br/>CLIENT IS LOCATED $client_offset_minutes MINUTES FROM UTC";
    echo "<br/>";
    echo "<br/>WHEN IT IS '$client_timestring' AT THE CLIENT, IT IS '$server_timestring' IN " . date_default_timezone_get();
}
// END OF PHP - USE HTML AND JS TO CREATE THE FORM
echo PHP_EOL; ?>
<form method="post">
<input name="date_O" id="dateTime_O" type="hidden" />
<input type="submit" value="CHECK CLIENT DATETIME" />
</form>
<!-- NOTE THIS WILL GIVE YOU THE VALUES AT PAGE-LOAD TIME, NOT AT SUBMIT TIME -->
<!-- MAN PAGE REF: http://www.w3schools.com/jsref/jsref_obj_date.asp -->
<script type="text/javascript">
var dateObject = new Date();
document.getElementById("dateTime_O").value = dateObject.getTimezoneOffset();
</script>

PHP作为服务器端,只能读取客户端发送的内容。如果没有用户(或像浏览器这样的用户代理)显式地告诉它,它不能告诉用户时区。

有几种猜测方法。您可以通过调用位置服务来查找IP并根据IP位置猜测时区(时间昂贵并且可能返回不正确的结果,我不建议这样做)。

你也可以让你的javascript做一个ajax调用告诉服务器Date().getTimezoneOffset()的值是什么。

相关内容

  • 没有找到相关文章

最新更新