user3980196
user3980196

Reputation: 533

PHP for loop not incrementing

I have the following PHP code:

$todaysdate = date_create();
for ($i = date_sub(date_create(),date_interval_create_from_date_string("1 month")); $i <= $todaysdate; $i = date_add($i,date_interval_create_from_date_string("1 day"))) {
    $json['message'][] = $i;
}

The counter $i doesnot increment at all. $i remains at Object { date="2017-03-02 20:00:55.000000", timezone_type=3, timezone="Asia/Kolkata"}

Upvotes: 0

Views: 243

Answers (1)

user3980196
user3980196

Reputation: 533

As suggested by RiggsFolly, I updated the code to the following:

$todaysdate = date_create();
for ($i = date_sub(date_create(),date_interval_create_from_date_string("1 month")); $i <= $todaysdate; $i = date_add($i,date_interval_create_from_date_string("+1 day"))) {
    $json['message'][] = $i->format('d-m-Y');
}

It's now working. The only change was the line $json['message'][] = $i->format('d-m-Y');

Upvotes: 1

Related Questions