#Carbon month setter

5 messages · Page 1 of 1 (latest)

tiny umbra
#

What would you expect the $date of the following to be?

$date = Carbon::parse('2024-12-31')->month(6)

Would it be:

  • a) 2024-06-31
  • b) 2024-06-30
  • c) 2024-07-01

|| I was expecting it to be B but apparently it's C. I hit a little gotcha in my code this morning :D||

open valve
#

We're doing quizzes now? 😂
But yes, those setters start counting from 0

vapid holly
#

Oh boy. This is why counting from the end of the month is never a good idea

#

DateTime is funky with adding months. It will increment the month, but if the month doesn't have that number of days, it moves it to the next month. And then it throws off the rest

#

So 2024-06-31 doesn't exist, and it becomes 2025-07-01