Joe
Joe

Reputation: 111

PHP >> Date Period Foreach Won't run

Can't work out what I'm missing here, can anyone help?

This should be simple, GET start_date & end_date, create DatePeriod() with interval 1 day, loop through.

but it won't work for some reason?

URL which loads is:

/forms/process_table_cumulative_averages.php?start_date=12-05-2015&end_date=19-05-2015&team_id=all

Code is as follows:

$start_date = new DateTime();
$end_date = new DateTime();

$start_date->createFromFormat('d/m/Y', $_GET['start_date']);
$end_date->createFromFormat('d/m/Y', $_GET['end_date']);

$team_id = $_GET['team_id'];

$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start_date, $interval, $end_date);

foreach ( $period as $datetime ){
    echo "hi\n";
}

Output is 204 No Content

:( Thanks

Upvotes: 0

Views: 410

Answers (1)

Honza Haering
Honza Haering

Reputation: 812

createFromFormat() is a static function that returns DateTime instance, so you must assign the result to your start and end variables:

$start_date = DateTime::createFromFormat('d-m-Y', $_GET['start_date']);
$end_date = DateTime::createFromFormat('d-m-Y', $_GET['end_date']);

Upvotes: 2

Related Questions