Skip to content
Advertisement

Laravel Carbon, problem with getting the last ten days

I need to get the date of the last ten days. To do so I do this

JavaScript

This is the result!!

2020-02-01 2020-02-02 2020-02-04 2020-02-07 2020-02-11 2020-02-16 2020-02-22 2020-02-29 2020-03-08 2020-03-17

Why it skips some days?

Advertisement

Answer

Because you’re adding 1 day, then 2 days, then 3 days, then 4 days… You should just add one day each time:

JavaScript

Outputs:

JavaScript

Edit: addDays() modifies the variable it is called on, so the code actually works without $day:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement