Closed morrislaptop closed 3 years ago
Hi, this can be a great addition indeed.
Are you sure we shouldn't use the next day though, for the end date?
Otherwise the last date is not included when you iterate the DatePeriod
object, which makes it behave differently from LocalDateRange
in this regard: https://3v4l.org/5Zggu
Hi, this can be a great addition indeed.
Are you sure we shouldn't use the next day though, for the end date?
Otherwise the last date is not included when you iterate the
DatePeriod
object, which makes it behave differently fromLocalDateRange
in this regard: https://3v4l.org/5Zggu
Hmmm I've added some assertions to verify the iterator and they seem to match up exactly. Looking into it further.
Ah yep, got you now. I feel like the essence of the library is inclusive, whereas \DatePeriod is exclusive. So there's a mismatch of ideas here.
What do you think users would expect?
In my scenario I just want to grab the end of the range in a DateTime context and stick it in a database.
Perhaps it might be better to do toEndDateTime()
and toStartDateTime()
which respect the inclusive nature of this library.
toDatePeriod
would be exclusive which would respect the exclusive nature of the native object.
Could this be configured with a flag?
public function toDatePeriod(bool $nextDay)
I'm afraid there isn't an ideal solution anyway, as you said there's a mismatch of ideas between brick's LocalDateRange
and DatePeriod
.
Actually, I don't see the issue here. Both objects produce the same result when iterating.
The example you provided had the end date at the start of the day, whereas this method sets the end date at the end of the day.
The native DatePeriod will include the last day, even if it's one second past the start of the day.
This is what the latest version of the tests ensure.
I think it's good to go as is :)
Hey @BenMorel , do you think you'll be merging this? I've implemented something similar over at https://github.com/spatie/period/pull/81
The native DatePeriod will include the last day, even if it's one second past the start of the day.
I missed this detail, thanks. One more gotcha with native DateTime classes ;-)
Please fix the few points I mentioned and it should be good to go.
Adds a
toDatePeriod
to theLocalDateRange
object which returns a native \DatePeriod PHP object.I opted to do end of day (instead of midnight next day) to be inline with the inclusive nature of LocalDateRange.
Note: This is not
bricks/date-time
Period class - I hope this doesn't create confusion.