briannesbitt / Carbon

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

Add option to include milliseconds in CarbonInterval::forHumans #2058

Closed pdscopes closed 4 years ago

pdscopes commented 4 years ago

Hello,

I encountered the following "issue" with the following code:

$seconds = 1.114;
$diffString = Carbon::createFromFormat('U.v', round($seconds, 3))->diffForHumans(
            Carbon::createFromTimestamp(0), Carbon::DIFF_ABSOLUTE, true, 2);

echo $diffString;

Carbon version: 2.30.0

PHP version: 7.3.15

I want to get:

1s 114ms

But I actually get:

1s

Thanks!

I have worked out a naive solution however this is not backwards compatible and I think it'd be a useful thing to be able to specify (maybe in the options) the smallest unit allowed:

diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php
--- a/src/Carbon/CarbonInterval.php
+++ b/src/Carbon/CarbonInterval.php
@@ -1156,10 +1156,11 @@
      $diffIntervalArray = [
            ['value' => $intervalValues->years,            'unit' => 'year',   'unitShort' => 'y'],
            ['value' => $intervalValues->months,           'unit' => 'month',  'unitShort' => 'm'],
            ['value' => $intervalValues->weeks,            'unit' => 'week',   'unitShort' => 'w'],
            ['value' => $intervalValues->daysExcludeWeeks, 'unit' => 'day',    'unitShort' => 'd'],
            ['value' => $intervalValues->hours,            'unit' => 'hour',   'unitShort' => 'h'],
            ['value' => $intervalValues->minutes,          'unit' => 'minute', 'unitShort' => 'min'],
            ['value' => $intervalValues->seconds,          'unit' => 'second', 'unitShort' => 's'],
+            ['value' => $intervalValues->milliseconds,     'unit' => 'millisecond', 'unitShort' => 'ms'],
        ];
kylekatarnls commented 4 years ago

Hello,

Those intervals are actually translated into hundred different languages according to ->locale() setting.

In this array unit and unitShort are in fact the key for translations you would find in the Lang directory such as randomly chinese: https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Lang/zh_Hans.php#L39

So a consistent change would also require to translate "ms" and millisecond" in every languages and for sure adding an option, 99% of the time diffForHumans() is used to show the age of a message/article or the remaining time before an event, and millisecond is nearly never relevant for those cases.

I'm open to pull-request for this feature.

Note: you could create the CarbonInterval directly rather than using Carbon and timestamps:

echo CarbonInterval::fromString('1.114 seconds')
  ->cascade()
  ->forHumans(Carbon::DIFF_ABSOLUTE, true, 2);
pdscopes commented 4 years ago

Great, thanks for the direct way of doing it.

I'll work on a pull request tomorrow. Not sure I'll be able to do all languages 100% accurately. I am going to keep it backwards compatible so the default smallest unit will be seconds.