briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.56k stars 1.28k forks source link

Issue with forHumans #2691

Closed lucraraujo closed 2 years ago

lucraraujo commented 2 years ago

Carbon version: 2.62.1 PHP version: 8.1.11

Hello,

I have a CarbonInterval that is a sum of many CarbonInterval objects. The sum object is this one (laravel dd)

Carbon\CarbonInterval {#308 ▼ // app/Http/Controllers/Controller.php:34
  interval: + 03:37:13.0
  #tzName: null
  #step: null
  #localMonthsOverflow: null
  #localYearsOverflow: null
  #localStrictModeEnabled: null
  #localHumanDiffOptions: null
  #localToStringFormat: null
  #localSerializer: null
  #localMacros: null
  #localGenericMacros: null
  #localFormatFunction: null
  #localTranslator: null
  +"y": 0
  +"m": 0
  +"d": 0
  +"h": 1
  +"i": 152
  +"s": 313
  +"f": 0.0
  +"weekday": 0
  +"weekday_behavior": 0
  +"first_last_day_of": 0
  +"invert": 0
  +"days": false
  +"special_type": 0
  +"special_amount": 0
  +"have_weekday_relative": 0
  +"have_special_relative": 0
}

When I call $object->forHumans() I get the string 1 hour 152 minutes 313 seconds.

I expected to get 3 hours 37 minutes 13 seconds.

I can see that a internal interval object store the time the way I want but I can't access it.

kylekatarnls commented 2 years ago

Helllo, this is the expected behavior, there are many cases where you don't want to resolve plus after the hour, the number of hours in day is not a constant (because of DST), days in months neither (28, 29, 30 or 31), so it has to be done with:

$object->cascade()->forHumans()

cascade then use factors that can be modified see setCascadeFactors in the doc https://carbon.nesbot.com/docs/#api-interval

PS: neither CarbonInterval (nor DateInterval AFAIK) store or calculate on the fly the cascaded interval, what appears here interval: + 03:37:13.0 is very likely done for debugging by the dd method.