Carbon Difference for Humans Anyone having any issues with carbon not rounding up when adding to dates? I've tried the ceil().
Examples:
// 11 months from now
Carbon::parse('2019-08-01 12:00:00')->addYear()->diffForHumans();
// 5 months from now
Carbon::parse('2019-08-01 12:00:00')->addMonths(6)->diffForHumans();
// 6 days from now
Carbon::parse('2019-08-01 12:00:00')->addDays(7)->diffForHumans();
etc...
Technically, it's lacking a couple of hours till the complete period. You can work around that by adding ->endOfDay() into the chain.
If I do
Carbon::now()->addSecond()->endOfDay()->diffForHumans();
Carbon::now()->addSeconds(60)->endOfDay()->diffForHumans();
Both return "3 hours from now"
Yes, because in both cases you make the date to be the last second of the day and it's 3 hours before the end of the day now ;)
It's actually the same result. I have a date, need to add time to it and have the diffInHumans() come back with the right info.
If I add a year in this example, and it returns 11 months from now, that's not correct. I'm sure it's returning 11.9474 or something like that and it's rounding down/floor etc...
Should it round up?
I thought it was just taking the first unit of the diff
eg 11months, 30 days, 12 hours = 11 months
5 hours, 59 minutes = 5 hours
You can specify the number of parts you want if that helps... no sign of rounding though
Yes, you're right it takes from (11m 28d 23:59:59.491142). Yeah going to have to work with it some more to get what I need.
Also get tricky with leap year etc. Have to think more about what I'm doing and change things up.
...
I think the better solution is to make the human in to more parts to get more accurate.
->diffForHumans([
'parts' => 2,
])
which will return "11 month 4 weeks from now"
See CarbonInterface::ROUND
$roundedDiff = $date2->diffForHumans($date1, [
'parts' => 1, // Toon slechts 1 onderdeel (dag, uur, minuut, etc.)
'options' => CarbonInterface::ROUND // Gebruik de afrondingsoptie
]);
Please sign in or create an account to participate in this conversation.