3

好吧,我对此束手无策。3天来一直试图解决这个问题,但我对此一无所获。

我需要获取$offset两个位置之间的值并将其从设定的时间(即(00:00).

这是我设置$offset值的方式,它工作得很好。

<?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($timezone1, $timezone2);

}

?>

这就是我尝试使用 DateTime 做我想做的事情的方法,这段代码只会回显该$offset值,而不会将其从 00:00 中删除

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

我什至尝试使用strtotime,但 strtotime 返回错误的值,stackoverflow 上的一些人建议我使用 DateTime。

<?php
$time1 = strtotime('00:00');

if (0 > $offset)
{
   // For negative offset (hours behind)
  $hour_dif = date('H:i', strtotime($time1 -$offset/3600));
  $time1 = "{$hour_dif}";
}
elseif (0 < $offset)
{
   // For positive offset (hours ahead)
     $hour_dif = date('H:i', strtotime($time1 +$offset/3600));
     $time1 = "{$hour_dif}";

}
else
{
   // For offsets in the same timezone.
   $time1 = "in the same timezone";
}

echo "{$time1}";
?>

请有人帮助我,因为这绝对是在浪费我的时间。

4

3 回答 3

0

这可以处理正偏移和负偏移:

$sign = $offset >= 0 ? 1 : -1;
$offset = $offset * $sign;  // make sure offset is a positive number

// set an object with the current date
$date = new DateTime();
$date->setTime(00, 00);

// the second date
$date2 = new DateTime();
$interval = new DateInterval("PT" . $offset . "S");
if ($sign > 0) {
    $date2->add($interval);
} else {
    $date2->sub($interval);
}

echo $date2->format("H:i");
于 2013-08-30T09:47:39.480 回答
0

以下代码将打印按小时数调整的输入时间$offset

$date = new DateTime();
$date->setTime(00, 00);

$date->add(DateInterval::createFromDateString("{$offset} hours"));

echo $date->format('H:i');

我再次测试了这个解决方案(使用 PHP 5.4.17),它正确地改变了时间。

PHPFiddle:http ://phpfiddle.org/main/code/rhd-pj4

于 2013-08-30T09:23:31.630 回答
0

您还可以使用Carbon轻松解决此问题,该类将大大简化任何类型的日期计算:

// example timezones
$timezone1 = 'Europe/Berlin'; 
$timezone2 = 'Asia/Yakutsk'; 

$dt1 = Carbon::createFromDate(2000, 1, 1, $timezone1);
$dt2 = Carbon::createFromDate(2000, 1, 1, $timezone2);

// false will force a relative difference, so it can be a negative result
$difference = $dt1->diffInMinutes($dt2, false);

$dtMidnight = Carbon::create(2000, 1, 1, 12, 0, 0);

// get the difference from midnight
$differenceFromMidnight = $dtMidnight->addMinutes($difference);

echo $differenceFromMidnight->hour;
echo $differenceFromMidnight->minute;
于 2013-08-30T09:33:55.877 回答