2017-12-15 4 views
0
$listData2 = []; 
$start  = new DateTime('2017-01-01'); 
$end  = (new DateTime('2017-01-12'))->modify('+1 day'); 
$interval = DateInterval::createFromDateString('1 day'); 
$period = new DatePeriod($start, $interval, $end); 

foreach ($period as $index => $period1) 
{ echo $listData2[] = $period1->format("d/m/Y"); } 

こんにちは、私は以下のコードを発見しました:2011年1月1日から12/01/2017まで私の日付を出力し、それはうまくいきます。上記のコードをPHP 5.5で使用しましたが、このコードをPHP 5.3を使用して別のコンピュータにコピーすると、エラーが発生します。 PHPと互換性があるように上記のコードを変更する方法phpの異なるバージョンの日付範囲をエコーする

答えて

1

[]を使用して配列を作成し、日付オブジェクトを初期化して操作することでこれを修正できます。私はhttp://sandbox.onlinephpfunctions.comでコードをテストしExample

<?php 
$listData2 = array(); 
$start  = new DateTime('2017-01-01'); 
$end  = new DateTime('2017-01-12'); 
$end  = $end->modify('+1 day'); 
$interval = DateInterval::createFromDateString('1 day'); 
$period = new DatePeriod($start, $interval, $end); 

foreach ($period as $index => $period1) { 
    echo $listData2[] = $period1->format("d/m/Y"); 
} 
0

- 5.3.0にダウン動作します。 この(new DateTime('2017-01-12'))->modify('+1 day');原因

syntax error, unexpected T_OBJECT_OPERATOR 

ソリューション

だけmodify('+1 day')を移動します。

<?php 
$listData2 = array(); 
$start  = new DateTime('2017-01-01'); 
$end  = new DateTime('2017-01-12'); 
$interval = DateInterval::createFromDateString('1 day'); 
$period = new DatePeriod($start, $interval, $end->modify('+1 day')); 

foreach ($period as $index => $period1) 
{ echo $listData2[] = $period1->format("d/m/Y") . "<br/>"; } 

ちょうどあなたがしたい場合は、ここでそれをテスト:http://sandbox.onlinephpfunctions.com/code/32f0fed1fb0c89c7bd3dff0ed21f9178ecca

関連する問題