I'm confused trying to add an create an array of date instances with carbon. What I'm trying to achieve is an array with one carbon object for each day in between 2 dates.
Here's what I have so far:
// Get oldest and newest date by sorting the array by created_at
usort($data, function($a, $b) {
return $a->created_at <=> $b->created_at;
});
$a = end($data);
$to = $a->created_at; //-> Newest date
$from = $data[0]->created_at; //-> Oldest date
// Work out the difference between to and from dates
$carbonTO = new Carbon($to);
$carbonFrom = new Carbon($from);
$diff = $carbonFrom->diffInDays($carbonTO);
// Write the dates to an array
$i = 0;
while ($diff >= 0) {
$filters[$i] = $carbonFrom->addDays($i);
$diff--;
$i++;
var_dump($filters);
}
die();
return $filters;
So the var_dump from within the loop echo's this:
array(1) {
[0]=> object(Carbon\Carbon)#238 (3) {
["date"]=> string(26) "2016-01-17 19:04:49.000000"
["timezone_type"]=> int(3)
["timezone"]=> string(3) "UTC"
}
}
array(2) {
[0]=> object(Carbon\Carbon)#238 (3) {
["date"]=> string(26) "2016-01-18 19:04:49.000000"
["timezone_type"]=> int(3)
["timezone"]=> string(3) "UTC"
}
[1]=> object(Carbon\Carbon)#238 (3) {
["date"]=> string(26) "2016-01-18 19:04:49.000000"
["timezone_type"]=> int(3)
["timezone"]=> string(3) "UTC"
}
}
As you can see the second time I output the array the key of 0 has been overwritten by the newer date of 2016-01-18. Anyone have any ideas why?
I'm running php 7.0.0 on mamp with Larvel 5.2.