首先,我已经挣扎了两天不停地弄清楚我需要使用这个代码并仍然停留在正方形。
所以我又来了,我希望有人能够解决这个问题,因为我的头脑正在爆炸。
我需要做的是在PHP中使用DateTime来区分两个值。
第一个值是00:00,这是一个设定值,永远不会改变。
第二个值是$ offset / 3600 * 1,这是两个时区之间的时差。
我正在使用的代码是:
<?php
if (0 > $offset)
{
// set an object with the current date
$date = new DateTime();
$date->setTime(00, 00);
// the second date
$date2 = new DateTime($offset/3600 * 1);
// apply the diff() method, getting a DateInterval object ($diDiff)
$diDiff = $date->diff($date2) ;
}
echo $diDiff->format("H:i");
?>
上面的代码是echo:H:i
而没有别的!
我做错了什么?
提前致谢。
修改
$ offset来自这里:
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($timezone1, $timezone2);
}
?>
timezone1和timezone2是两个下拉列表,里面有php时区,它们是相同的。
像这样: <select name="timezone2" id="timezone2" class="timezone2">
<?php
foreach($options as $key => $value)
{
echo '<option value="'. $key .'" label="'. $value .'">'.$value.'</option>';
}
?>
<option value="Africa/Abidjan" label="Abidjan">Abidjan</option>
<option value="Africa/Accra" label="Accra">Accra</option>
<option value="Africa/Addis_Ababa" label="Addis Ababa">Addis Ababa</option>
<option value="Africa/Algiers" label="Algiers">Algiers</option>
<option value="Africa/Asmara" label="Asmara">Asmara</option>
</select>
答案 0 :(得分:2)