2013-01-15 15 views
0

您好,我有一个像欧洲/柏林这样的特殊时区,我想在当前日期添加90天。之后,我发现了这个月的最后一天。php datetime/datetimezone substract 2小时而不是1

数据库中的我的日期以UTC保存。这就是为什么我最终必须改变时区。

我的问题是,时区更改从欧​​洲/柏林和UTC之间的时间减去2小时。当我在修改UTC之前不修改日期时间对象时,它只会减去1小时。有人可以告诉我问题是什么或正确的解决方案吗?

//$timezone = 'Europe/Berlin'; 
private function getMaxTillDate($timezone) 
{   
    $threemonth = new \DateTime('now', new \DateTimeZone($timezone)); 
    $threemonth->setTime(23, 59, 59); 
    $threemonth->add(new \DateInterval('P0Y90DT0H0M')); 
    $maxday = $threemonth->format('t');   
    $threemonth->setDate($threemonth->format('Y'), $threemonth->format('m'), $maxday); 
    $threemonth->setTimezone(new \DateTimeZone('UTC')); 

    return $threemonth; 
} 

非常感谢您

+2

有夏令时在德国至少半年,即UTC + 2小时。 – Sven

+0

是的,我知道,但它没有描述为什么PHP只减少1小时,如果我没有操纵dateobject之前我改变时区。 –

回答

0

我测试你的代码 - 它为我工作:

function getMaxTillDate($timezone){ 
    $threemonth = new \DateTime('now', new \DateTimeZone($timezone)); 
    var_dump(1, $threemonth); 
    $threemonth->setTime(23, 59, 59); 
    var_dump(2, $threemonth); 
    $threemonth->add(new \DateInterval('P0Y90DT0H0M')); 
    var_dump(3, $threemonth); 
    $maxday = $threemonth->format('t'); 
    var_dump(4, $threemonth, $maxday); 
    $threemonth->setDate($threemonth->format('Y'), $threemonth->format('m'), $maxday); 
    var_dump(5, $threemonth); 
    $threemonth->setTimezone(new \DateTimeZone('UTC')); 
    var_dump(6, $threemonth); 
    return $threemonth; 
} 

$tz = "Europe/Berlin"; 

var_dump(getMaxTillDate($tz)); 

结果:

int(1) 
class DateTime#1 (3) { 
public $date => 
string(19) "2013-01-15 22:29:59" 
public $timezone_type => 
int(3) 
public $timezone => 
string(13) "Europe/Berlin" 
} 
int(2) 
class DateTime#1 (3) { 
public $date => 
string(19) "2013-01-15 23:59:59" 
public $timezone_type => 
int(3) 
public $timezone => 
string(13) "Europe/Berlin" 
} 
int(3) 
class DateTime#1 (3) { 
public $date => 
string(19) "2013-04-15 23:59:59" 
public $timezone_type => 
int(3) 
public $timezone => 
string(13) "Europe/Berlin" 
} 
int(4) 
class DateTime#1 (3) { 
public $date => 
string(19) "2013-04-15 23:59:59" 
public $timezone_type => 
int(3) 
public $timezone => 
string(13) "Europe/Berlin" 
} 
string(2) "30" 
int(5) 
class DateTime#1 (3) { 
public $date => 
string(19) "2013-04-30 23:59:59" 
public $timezone_type => 
int(3) 
public $timezone => 
string(13) "Europe/Berlin" 
} 
int(6) 
class DateTime#1 (3) { 
public $date => 
string(19) "2013-04-30 21:59:59" 
public $timezone_type => 
int(3) 
public $timezone => 
string(3) "UTC" 
} 
class DateTime#1 (3) { 
public $date => 
string(19) "2013-04-30 21:59:59" 
public $timezone_type => 
int(3) 
public $timezone => 
string(3) "UTC" 
} 
+0

请取消注释这些行,然后您将看到不同之处:$ threemonth-> setTime(23,59,59); $ threemonth-> add(new \ DateInterval('P0Y90DT0H0M')); $ maxday = $ threemonth-> format('t'); $ threemonth-> setDate($ threemonth-> format('Y'),$ threemonth-> format('m'),$ maxday); –

+0

他们没有评论,如你所见。如果我将它们评论出来,我完全可以看到当前的时区设置处于活动状态:现在在今天的“欧洲/柏林”时区22:50,我们正在冬天,例如, “UTC + 1”。从2013-03-31开始,我们将在“UTC + 2”上。 – Sven

+0

它不能解释为什么时区减少2小时,当我操纵日期和只有1小时,当我不做任何事情的日期。 –