2013-10-03 84 views
1

我正在上載項目到具有舊版本PHP的服務器,該版本不支持DateTimeadd()函數。向PHP 5.3之前的DateTime對象添加間隔

所以這個代碼:

$tomorrow = $today->add(new DateInterval("P1D")); 

生成此錯誤的服務器日誌:

PHP Fatal error: Call to undefined method DateTime::add() 

我試圖找到其中涉及strtotime一個解決方案,但我不斷收到錯誤。有一個簡單的解決方法,我可以使用嗎?

+0

的strtotime()的罰款。你會得到什麼錯誤? – ChristopheBrun

+2

您可以使用[DateTime :: modify](http://www.php.net/manual/en/datetime.modify.php)而不是'DateTime :: add()'或'DateTime :: sub()' 。 –

回答

3

可以加/減與DateTime::modify()(PHP 5> = 5.2.0) 代替DateTime::add()DateTime::sub()日期(PHP 5> = 5.3.0)

相反的:

$tomorrow = $today->add(new DateInterval('P1D')); 

你可以使用:

$tomorrow = $today->modify('+1 day'); 
1
//Create array with all dates within date span 
$begin = new DateTime($start_date); 
$end = new DateTime(date("Y-m-d",strtotime("+1 day", strtotime($end_date)))); 
while($begin < $end) { 
    $period[] = $begin->format('Y-m-d'); 
    $begin->modify('+1 day'); 
} 
+0

你爲什麼要將'strtotime'與DateTime類混合? –