Repository: cakephp/chronos (php)

Issue:
diffForHumans() gives misleading results for future dates
Consider the following example of a future date two weeks ahead of today's date:

`echo Chronos::parse(new DateTime('2 weeks'))->diffForHumans();`

>1 week from now

It seems to me the expected out put should be _two weeks_ from now.

Also consider that as the current time ticks forward, the diff should probably not flip from two weeks to one week, since this is not very granular output. That is, once the difference becomes less than two weeks, it should probably change scale to days, e.g. 11 days, 10 days, etc. If it jumps straight from two weeks to one week, when should this occur? One second prior to the full two weeks? Half way between one week and two weeks? Up to one second after one full week? This presents a boundary condition problem that is difficult to reason about if we do not switch to a more granular time output, e.g. from weeks to days.

As an aside, the behaviour is not the same for past dates:

`echo Chronos::parse(new DateTime('-2 weeks'))->diffForHumans();`

>2 weeks ago



Pull request description:
Fix: Unexpected Human Interval Values
`diffForHumans()` is returning "not very human" intervals for unit values. Per the discussion this tries to identify a standard and  updates the values to match that.

Fixes #307

Patch:
diff --git a/src/DifferenceFormatter.php b/src/DifferenceFormatter.php
index d1a47cd..179dd79 100644
--- a/src/DifferenceFormatter.php
+++ b/src/DifferenceFormatter.php
@@ -64,17 +64,17 @@ class DifferenceFormatter implements DifferenceFormatterInterface
                 $unit = 'year';
                 $count = $diffInterval->y;
                 break;
-            case $diffInterval->m > 0:
+            case $diffInterval->days >= ChronosInterface::DAYS_PER_WEEK * 8:
                 $unit = 'month';
                 $count = $diffInterval->m;
                 break;
+            case $diffInterval->days >= ChronosInterface::DAYS_PER_WEEK * 3:
+                $unit = 'week';
+                $count = (int)($diffInterval->days / ChronosInterface::DAYS_PER_WEEK);
+                break;
             case $diffInterval->d > 0:
                 $unit = 'day';
                 $count = $diffInterval->d;
-                if ($count >= ChronosInterface::DAYS_PER_WEEK) {
-                    $unit = 'week';
-                    $count = (int)($count / ChronosInterface::DAYS_PER_WEEK);
-                }
                 break;
             case $diffInterval->h > 0:
                 $unit = 'hour';


Test patch:
diff --git a/tests/TestCase/DateTime/DiffTest.php b/tests/TestCase/DateTime/DiffTest.php
index 86a03cd..bd984a8 100644
--- a/tests/TestCase/DateTime/DiffTest.php
+++ b/tests/TestCase/DateTime/DiffTest.php
@@ -780,9 +780,11 @@ class DiffTest extends TestCase
             [$now, $now->addYears(11), '11 years before'],
             [$now, $now->addYears(1), '1 year before'],
             [$now, $now->addMonths(11), '11 months before'],
-            [$now, $now->addMonths(1), '1 month before'],
-            [$now, $now->addDays(8), '1 week before'],
+            [$now, $now->addMonths(1), '4 weeks before'],
             [$now, $now->addDays(23), '3 weeks before'],
+            [$now, $now->addDays(21), '3 weeks before'],
+            [$now, $now->addDays(20), '20 days before'],
+            [$now, $now->addDays(8), '8 days before'],
             [$now, $now->addDays(1), '1 day before'],
             [$now, $now->addDays(6), '6 days before'],
             [$now, $now->addHours(1), '1 hour before'],
@@ -798,9 +800,11 @@ class DiffTest extends TestCase
             [$now, $now->subYears(11), '11 years after'],
             [$now, $now->subYears(1), '1 year after'],
             [$now, $now->subMonths(11), '11 months after'],
-            [$now, $now->subMonths(1), '1 month after'],
-            [$now, $now->subDays(8), '1 week after'],
+            [$now, $now->subMonths(1), '4 weeks after'],
             [$now, $now->subDays(23), '3 weeks after'],
+            [$now, $now->subDays(21), '3 weeks after'],
+            [$now, $now->subDays(20), '20 days after'],
+            [$now, $now->subDays(8), '8 days after'],
             [$now, $now->subDays(1), '1 day after'],
             [$now, $now->subDays(6), '6 days after'],
             [$now, $now->subHours(1), '1 hour after'],