Carbon: diffInDays and diffForHumans not returning expected result

Created on 17 Dec 2015  路  9Comments  路  Source: briannesbitt/Carbon

Hey,

I encountered that if the "diffInDays()" returns a value between 7 and 14, in this case 13 days, the "diffForHumans()" returns 1 week, and not 2 weeks, whereas it's closer to 2 weeks, than 1 week?

I recall it being able to handle this properly, can't 100% confirm it though.

Is it possible to achieve a similar result as the one in the last comment, the "extended" diffForHumans(), and still keeping it as dynamic, as it is now?

[3] > $now = Carbon\Carbon::now()->addDays(13); $now->diffForHumans();
// '1 week from now' // Actual is 1 week and 6 days
[4] > $now = Carbon\Carbon::now()->addDays(14); $now->diffForHumans();
// '2 weeks from now' // Actual is 2 weeks
[5] > $now = Carbon\Carbon::now()->addDays(15); $now->diffForHumans();
// '2 weeks from now' // Actual is 2 weeks and 1 day

Most helpful comment

Merged and multiparts now supported.

All 9 comments

This is a big thing for me too. I see this type of thing all over the internet (Github, Facebook) where their timestamps round down or up too early or late.

In my circumstance, I want to show the connecting time between the arrival and departure of two flights. diffForHumans() always shows the highest unit, so 1h 55m shows as 1 hour, but this is misleading and inaccurate. It would be a very useful feature to extend this method to show the _exact_ difference.

I've found that the solution would be to use CarbonInterval. What I really want to do is below, but creating a CarbonInterval instance from a DateInterval instance that came from Carbon is specifically denied: https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/CarbonInterval.php#L230

$diffInterval = $date->diff($date2); //Instance of DateInterval

echo Carbon\CarbonInterval::instance($diffInterval)->forHumans; //Illegal

This is a problem for me as well. It would be great if there was a new method, or a parameter within an existing method to increase the granularity of the diff to include minutes, or round to the nearest hour, day, week etc.

diffForHuman() currently works like this:

Carbon::now()->subDays(7)->diffForHumans();               // 1 week ago 
Carbon::now()->subDays(13)->diffForHumans();               // 1 week ago 

I think it's more relevant if we round it to the higher unit if it is superior to .5 (ie: more than 1.5 weeks should render 2 weeks).

Alternatively (better option IMHO), I think that if the unit of time (days/weeks/months/year) is less than 2 (or maybe 3 [it should probably be in a configuration option]), it is better to render it in the inferior unit. For example:

Carbon::now()->subHours(36)->diffForHumans() // 36 hours ago [instead of 1 day ago]
Carbon::now()->subDays(7)->diffForHumans() // 7 days ago [instead of 1 week ago]
Carbon::now()->subDays(13)->diffForHumans() // 13 days ago [instead of 1 week ago]
Carbon::now()->subDays(14)->diffForHumans() // 2 weeks ago 
Carbon::now()->addMonths(23)->diffForHumans() // 23 months from now [instead of 1 year from now]

This great pull request solve the problem in a different way : https://github.com/briannesbitt/Carbon/pull/772

Hope it will be merge soon.

Merged and multiparts now supported.

FWIW I wanted rounding too and did a little write-up about it here: https://github.com/tylercollier/carbon-interval-rounding

If you use composer, you should not modify manually your files in your vendor. There are multiple problems with this approach:

  • updates will erase the modification and force you to update your project,
  • if someone else do the same, you must choose your change or his change, but can't have both,
  • it makes it difficult to see the code difference (comparing to a fork or an sub-class using extends)
  • either you need to version it (thousand more problem), or you need to every of your colleagues to do the same modification,
  • changing such a behavior can break third-party libraries that use Carbon and may rely on it.

If you want a new behavior, you should propose an option to make it explicit and propose a pull-request, so it can be officially embedded in a next version.

The ability to round is now available as part of release 2.27 and has multiple rounding options like ROUND, CEIL, FLOOR. See https://github.com/briannesbitt/Carbon/releases/tag/2.27.0; it pulls in PR #1930. Thanks @kylekatarnls!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ManojKiranA picture ManojKiranA  路  4Comments

Yo1L picture Yo1L  路  5Comments

chanux picture chanux  路  5Comments

kylekatarnls picture kylekatarnls  路  5Comments

MaxGiting picture MaxGiting  路  4Comments