我一直在试图解决这个问题,并花了几个小时来找出这种情况一直发生的原因,但没有结果。
我真的不知道我做错了什么!
我试图使用PHP中的时区创建一个简单的时差。我需要允许用户选择页面上的两个位置。为此,我使用了两个下拉列表,其中包含所有的时区。
每次在服务器上运行代码/页时,都会得到以下错误:
致命错误:在/home/a1385482/public_html/timeDiff.php中带有消息'DateTimeZone::__construct() datetimezone‘的未命名异常“异常”--构造:未知时区或坏时区(Origin_tz) 堆栈跟踪: #0 /home/a1385482/public_html/timediff.php(204):DateTimeZone->__construct('origin_tz') #1 /home/a1385482/public_html/timediff.php(215):get_timezone_offset(“remote_tz”) #2 {main}抛入/home/a1385482/public_html/timediff.php第204行
以下是完整的页面代码:
<?php
echo "Version: " . phpversion();
?>
<form method="post" action="">
<select name="timezone2" id="timezone2" class="timezone2">
<option value="Pacific/Midway">(GMT-11:00) Midway Island, Samoa</option>
.. snip ..
</select>
<select name="timezone1" id="timezone1" class="timezone1">
<option value="Pacific/Midway">(GMT-11:00) Midway Island, Samoa</option>
.. snip ..
</select>
<input type="submit" name="submit" value="send"/>
</form>
<?php
if( isset($_POST['submit']))
{
//be sure to validate and clean your variables
$timezone1 = htmlentities($_POST['timezone1']);
$timezone2 = htmlentities($_POST['timezone2']);
//then you can use them in a PHP function.
function get_timezone_offset( $origin_tz, $remote_tz ) {
$timezone1 = new DateTimeZone( $origin_tz );
$timezone2 = new DateTimeZone( $remote_tz );
$datetime1 = new DateTime("now", $timezone1);
$datetime2 = new DateTime("now", $timezone2);
$offset = $timezone1->getOffset($datetime1) - $timezone2->getOffset($datetime2);
return $offset;
}
$offset = get_timezone_offset('origin_tz', 'remote_tz');
echo $offset/3600;
}
?>有人能帮我照亮一下吗?因为我被困在这上面快8个小时了。
发布于 2013-08-21 18:29:42
将'origin_tz‘和'remote_tz’传递给构造函数,构造函数不是有效的时区,参见:http://www.php.net/manual/en/timezones.php
我想你想:
$offset = get_timezone_offset($timezone1, $timezone2);https://stackoverflow.com/questions/18364946
复制相似问题