Closed pupitooo closed 5 years ago
Hi, Period cannot return the total number of days, as it depends on the year/month it's applied to.
For example:
Even though your use case is legitimate, it therefore does not belong to Period.
Your approach is the correct one:
$days = $until->toEpochDay() - $today->toEpochDay();
Do you feel like there should be a function such as LocalDate::daysUntil(LocalDate $date) : int
?
I forgot to mention that this is because until()
calculates a period in terms of years, months and days. The alternative would've been to return a Period with only the days
set, and zero years
and months
.
I checked what Java does, and they provide 2 methods:
Period until(ChronoLocalDate endDateExclusive)
Calculates the period between this date and another date as a Period.
long until(Temporal endExclusive, TemporalUnit unit)
Calculates the amount of time until another date in terms of the specified unit.
The first method does the same as the current implementation, but it looks like the second method can be used to get the result in terms of years, months or days individually (as an int, not as a Period, though).
I'm not sure we want to follow this generic route, but I'm not against adding a method like LocalDate::daysUntil()
. Thoughts?
The alternative would be to add an optional bool
parameter to until()
, to allow it to return a Period that only contains days. I'm not sure what this is worth.
Just now I am using approach
$days = $until->toEpochDay() - $today->toEpochDay();
but it contains counting in my code. And this is not so easy to use (you have to think about order of items).
I preffer LocalDate::daysUntil()
because bool parameter switch behaviour of all result. Separated function will be little bit more transparent.
I miss any funcion to get real days. It's not possible to get real days from Period.