PHP DatePeriod error when using DateInterval to loop over months -
i using dateinterval object loop on months. when running following code below:
$interval = new dateinterval('p1m'); $daterange = new dateperiod('2017-08-01', $interval , '2017-10-01'); foreach($daterange $d){ $start_date = $d->format("y-m-d"); echo $start_date; } //expected output: // 2017-08-01 // 2017-09-01
gives following error:
fatal error: uncaught exception 'exception' message 'dateperiod::__construct(): constructor accepts either (datetimeinterface, dateinterval, int) or (datetimeinterface, dateinterval, datetime) or (string) arguments.' in c:.. stack trace: #0 c:\..: dateperiod->__construct('2017-08-01', object(dateinterval), '2017-10-01') #1 {main} thrown in c:\..
how can expected output?
the dateperiod
constructor not used correctly. see official reference dateperiod.
try this:
$daterange = new dateperiod( new datetime('2017-08-01'), $interval , new datetime('2017-10-01') );
Comments
Post a Comment