2013-01-15 22 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小時,當我不做任何事情的日期。 –