Skip to content

Commit

Permalink
Merge pull request #1240 from kylekatarnls/diff-options
Browse files Browse the repository at this point in the history
Diff options
  • Loading branch information
kylekatarnls authored Apr 16, 2018
2 parents e4b3507 + a4ee1de commit bab61b2
Show file tree
Hide file tree
Showing 30 changed files with 524 additions and 355 deletions.
69 changes: 68 additions & 1 deletion src/Carbon/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
*/
class Carbon extends DateTime implements JsonSerializable
{
const NO_ZERO_DIFF = 01;
const JUST_NOW = 02;
const ONE_DAY_WORDS = 04;
const TWO_DAY_WORDS = 010;

/**
* The day constants.
*/
Expand Down Expand Up @@ -270,6 +275,45 @@ class Carbon extends DateTime implements JsonSerializable
*/
protected static $yearsOverflow = true;

/**
* Options for diffForHumans().
*
* @var int
*/
protected static $humanDiffOptions = self::NO_ZERO_DIFF;

/**
* @param int $humanDiffOptions
*/
public static function setHumanDiffOptions($humanDiffOptions)
{
static::$humanDiffOptions = $humanDiffOptions;
}

/**
* @param int $humanDiffOption
*/
public static function enableHumanDiffOption($humanDiffOption)
{
static::$humanDiffOptions = static::getHumanDiffOptions() | $humanDiffOption;
}

/**
* @param int $humanDiffOption
*/
public static function disableHumanDiffOption($humanDiffOption)
{
static::$humanDiffOptions = static::getHumanDiffOptions() & ~$humanDiffOption;
}

/**
* @return int
*/
public static function getHumanDiffOptions()
{
return static::$humanDiffOptions;
}

/**
* Add microseconds to now on PHP < 7.1 and 7.1.3 if set to true,
* let microseconds to 0 on those PHP versions if false.
Expand Down Expand Up @@ -3606,7 +3650,14 @@ public function diffForHumans($other = null, $absolute = false, $short = false,
}

if (count($interval) === 0) {
$count = 1;
if ($isNow && static::getHumanDiffOptions() & self::JUST_NOW) {
$key = 'diff_now';
$translation = static::translator()->trans($key);
if ($translation !== $key) {
return $translation;
}
}
$count = static::getHumanDiffOptions() & self::NO_ZERO_DIFF ? 1 : 0;
$unit = $short ? 's' : 'second';
$interval[] = static::translator()->transChoice($unit, $count, array(':count' => $count));
}
Expand All @@ -3625,6 +3676,22 @@ public function diffForHumans($other = null, $absolute = false, $short = false,
$transId = $isNow ? ($isFuture ? 'from_now' : 'ago') : ($isFuture ? 'after' : 'before');

if ($parts === 1) {
if ($isNow && $unit === 'day') {
if ($count === 1 && static::getHumanDiffOptions() & self::ONE_DAY_WORDS) {
$key = $isFuture ? 'diff_tomorrow' : 'diff_yesterday';
$translation = static::translator()->trans($key);
if ($translation !== $key) {
return $translation;
}
}
if ($count === 2 && static::getHumanDiffOptions() & self::TWO_DAY_WORDS) {
$key = $isFuture ? 'diff_after_tomorrow' : 'diff_before_yesterday';
$translation = static::translator()->trans($key);
if ($translation !== $key) {
return $translation;
}
}
}
// Some langs have special pluralization for past and future tense.
$key = $unit.'_'.$transId;
$count = isset($count) ? $count : 1;
Expand Down
28 changes: 14 additions & 14 deletions src/Carbon/Lang/af.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*/

return array(
'year' => '1 jaar|:count jare',
'y' => '1 jaar|:count jare',
'month' => '1 maand|:count maande',
'm' => '1 maand|:count maande',
'week' => '1 week|:count weke',
'w' => '1 week|:count weke',
'day' => '1 dag|:count dae',
'd' => '1 dag|:count dae',
'hour' => '1 uur|:count ure',
'h' => '1 uur|:count ure',
'minute' => '1 minuut|:count minute',
'min' => '1 minuut|:count minute',
'second' => '1 sekond|:count sekondes',
's' => '1 sekond|:count sekondes',
'year' => ':count jaar|:count jare',
'y' => ':count jaar|:count jare',
'month' => ':count maand|:count maande',
'm' => ':count maand|:count maande',
'week' => ':count week|:count weke',
'w' => ':count week|:count weke',
'day' => ':count dag|:count dae',
'd' => ':count dag|:count dae',
'hour' => ':count uur|:count ure',
'h' => ':count uur|:count ure',
'minute' => ':count minuut|:count minute',
'min' => ':count minuut|:count minute',
'second' => ':count sekond|:count sekondes',
's' => ':count sekond|:count sekondes',
'ago' => ':time terug',
'from_now' => ':time van nou af',
'after' => ':time na',
Expand Down
28 changes: 14 additions & 14 deletions src/Carbon/Lang/bg.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*/

return array(
'year' => '1 година|:count години',
'y' => '1 година|:count години',
'month' => '1 месец|:count месеца',
'm' => '1 месец|:count месеца',
'week' => '1 седмица|:count седмици',
'w' => '1 седмица|:count седмици',
'day' => '1 ден|:count дни',
'd' => '1 ден|:count дни',
'hour' => '1 час|:count часа',
'h' => '1 час|:count часа',
'minute' => '1 минута|:count минути',
'min' => '1 минута|:count минути',
'second' => '1 секунда|:count секунди',
's' => '1 секунда|:count секунди',
'year' => ':count година|:count години',
'y' => ':count година|:count години',
'month' => ':count месец|:count месеца',
'm' => ':count месец|:count месеца',
'week' => ':count седмица|:count седмици',
'w' => ':count седмица|:count седмици',
'day' => ':count ден|:count дни',
'd' => ':count ден|:count дни',
'hour' => ':count час|:count часа',
'h' => ':count час|:count часа',
'minute' => ':count минута|:count минути',
'min' => ':count минута|:count минути',
'second' => ':count секунда|:count секунди',
's' => ':count секунда|:count секунди',
'ago' => 'преди :time',
'from_now' => ':time от сега',
'after' => 'след :time',
Expand Down
28 changes: 14 additions & 14 deletions src/Carbon/Lang/ca.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*/

return array(
'year' => '1 any|:count anys',
'y' => '1 any|:count anys',
'month' => '1 mes|:count mesos',
'm' => '1 mes|:count mesos',
'week' => '1 setmana|:count setmanes',
'w' => '1 setmana|:count setmanes',
'day' => '1 dia|:count dies',
'd' => '1 dia|:count dies',
'hour' => '1 hora|:count hores',
'h' => '1 hora|:count hores',
'minute' => '1 minut|:count minuts',
'min' => '1 minut|:count minuts',
'second' => '1 segon|:count segons',
's' => '1 segon|:count segons',
'year' => ':count any|:count anys',
'y' => ':count any|:count anys',
'month' => ':count mes|:count mesos',
'm' => ':count mes|:count mesos',
'week' => ':count setmana|:count setmanes',
'w' => ':count setmana|:count setmanes',
'day' => ':count dia|:count dies',
'd' => ':count dia|:count dies',
'hour' => ':count hora|:count hores',
'h' => ':count hora|:count hores',
'minute' => ':count minut|:count minuts',
'min' => ':count minut|:count minuts',
'second' => ':count segon|:count segons',
's' => ':count segon|:count segons',
'ago' => 'fa :time',
'from_now' => 'dins de :time',
'after' => ':time després',
Expand Down
28 changes: 14 additions & 14 deletions src/Carbon/Lang/cs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*/

return array(
'year' => '1 rok|:count roky|:count let',
'y' => '1 rok|:count roky|:count let',
'month' => '1 měsíc|:count měsíce|:count měsíců',
'm' => '1 měsíc|:count měsíce|:count měsíců',
'week' => '1 týden|:count týdny|:count týdnů',
'w' => '1 týden|:count týdny|:count týdnů',
'day' => '1 den|:count dny|:count dní',
'd' => '1 den|:count dny|:count dní',
'hour' => '1 hodinu|:count hodiny|:count hodin',
'h' => '1 hodinu|:count hodiny|:count hodin',
'minute' => '1 minutu|:count minuty|:count minut',
'min' => '1 minutu|:count minuty|:count minut',
'second' => '1 sekundu|:count sekundy|:count sekund',
's' => '1 sekundu|:count sekundy|:count sekund',
'year' => ':count rok|:count roky|:count let',
'y' => ':count rok|:count roky|:count let',
'month' => ':count měsíc|:count měsíce|:count měsíců',
'm' => ':count měsíc|:count měsíce|:count měsíců',
'week' => ':count týden|:count týdny|:count týdnů',
'w' => ':count týden|:count týdny|:count týdnů',
'day' => ':count den|:count dny|:count dní',
'd' => ':count den|:count dny|:count dní',
'hour' => ':count hodinu|:count hodiny|:count hodin',
'h' => ':count hodinu|:count hodiny|:count hodin',
'minute' => ':count minutu|:count minuty|:count minut',
'min' => ':count minutu|:count minuty|:count minut',
'second' => ':count sekundu|:count sekundy|:count sekund',
's' => ':count sekundu|:count sekundy|:count sekund',
'ago' => ':time nazpět',
'from_now' => 'za :time',
'after' => ':time později',
Expand Down
28 changes: 14 additions & 14 deletions src/Carbon/Lang/da.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*/

return array(
'year' => '1 år|:count år',
'y' => '1 år|:count år',
'month' => '1 måned|:count måneder',
'm' => '1 måned|:count måneder',
'week' => '1 uge|:count uger',
'w' => '1 uge|:count uger',
'day' => '1 dag|:count dage',
'd' => '1 dag|:count dage',
'hour' => '1 time|:count timer',
'h' => '1 time|:count timer',
'minute' => '1 minut|:count minutter',
'min' => '1 minut|:count minutter',
'second' => '1 sekund|:count sekunder',
's' => '1 sekund|:count sekunder',
'year' => ':count år|:count år',
'y' => ':count år|:count år',
'month' => ':count måned|:count måneder',
'm' => ':count måned|:count måneder',
'week' => ':count uge|:count uger',
'w' => ':count uge|:count uger',
'day' => ':count dag|:count dage',
'd' => ':count dag|:count dage',
'hour' => ':count time|:count timer',
'h' => ':count time|:count timer',
'minute' => ':count minut|:count minutter',
'min' => ':count minut|:count minutter',
'second' => ':count sekund|:count sekunder',
's' => ':count sekund|:count sekunder',
'ago' => ':time siden',
'from_now' => 'om :time',
'after' => ':time efter',
Expand Down
44 changes: 22 additions & 22 deletions src/Carbon/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
*/

return array(
'year' => '1 Jahr|:count Jahre',
'y' => '1J|:countJ',
'month' => '1 Monat|:count Monate',
'm' => '1Mon|:countMon',
'week' => '1 Woche|:count Wochen',
'w' => '1Wo|:countWo',
'day' => '1 Tag|:count Tage',
'd' => '1Tg|:countTg',
'hour' => '1 Stunde|:count Stunden',
'h' => '1Std|:countStd',
'minute' => '1 Minute|:count Minuten',
'min' => '1Min|:countMin',
'second' => '1 Sekunde|:count Sekunden',
's' => '1Sek|:countSek',
'year' => ':count Jahr|:count Jahre',
'y' => ':countJ|:countJ',
'month' => ':count Monat|:count Monate',
'm' => ':countMon|:countMon',
'week' => ':count Woche|:count Wochen',
'w' => ':countWo|:countWo',
'day' => ':count Tag|:count Tage',
'd' => ':countTg|:countTg',
'hour' => ':count Stunde|:count Stunden',
'h' => ':countStd|:countStd',
'minute' => ':count Minute|:count Minuten',
'min' => ':countMin|:countMin',
'second' => ':count Sekunde|:count Sekunden',
's' => ':countSek|:countSek',
'ago' => 'vor :time',
'from_now' => 'in :time',
'after' => ':time später',
'before' => ':time zuvor',

'year_from_now' => '1 Jahr|:count Jahren',
'month_from_now' => '1 Monat|:count Monaten',
'week_from_now' => '1 Woche|:count Wochen',
'day_from_now' => '1 Tag|:count Tagen',
'year_ago' => '1 Jahr|:count Jahren',
'month_ago' => '1 Monat|:count Monaten',
'week_ago' => '1 Woche|:count Wochen',
'day_ago' => '1 Tag|:count Tagen',
'year_from_now' => ':count Jahr|:count Jahren',
'month_from_now' => ':count Monat|:count Monaten',
'week_from_now' => ':count Woche|:count Wochen',
'day_from_now' => ':count Tag|:count Tagen',
'year_ago' => ':count Jahr|:count Jahren',
'month_ago' => ':count Monat|:count Monaten',
'week_ago' => ':count Woche|:count Wochen',
'day_ago' => ':count Tag|:count Tagen',
);
28 changes: 14 additions & 14 deletions src/Carbon/Lang/el.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*/

return array(
'year' => '1 χρόνος|:count χρόνια',
'y' => '1 χρόνος|:count χρόνια',
'month' => '1 μήνας|:count μήνες',
'm' => '1 μήνας|:count μήνες',
'week' => '1 εβδομάδα|:count εβδομάδες',
'w' => '1 εβδομάδα|:count εβδομάδες',
'day' => '1 μέρα|:count μέρες',
'd' => '1 μέρα|:count μέρες',
'hour' => '1 ώρα|:count ώρες',
'h' => '1 ώρα|:count ώρες',
'minute' => '1 λεπτό|:count λεπτά',
'min' => '1 λεπτό|:count λεπτά',
'second' => '1 δευτερόλεπτο|:count δευτερόλεπτα',
's' => '1 δευτερόλεπτο|:count δευτερόλεπτα',
'year' => ':count χρόνος|:count χρόνια',
'y' => ':count χρόνος|:count χρόνια',
'month' => ':count μήνας|:count μήνες',
'm' => ':count μήνας|:count μήνες',
'week' => ':count εβδομάδα|:count εβδομάδες',
'w' => ':count εβδομάδα|:count εβδομάδες',
'day' => ':count μέρα|:count μέρες',
'd' => ':count μέρα|:count μέρες',
'hour' => ':count ώρα|:count ώρες',
'h' => ':count ώρα|:count ώρες',
'minute' => ':count λεπτό|:count λεπτά',
'min' => ':count λεπτό|:count λεπτά',
'second' => ':count δευτερόλεπτο|:count δευτερόλεπτα',
's' => ':count δευτερόλεπτο|:count δευτερόλεπτα',
'ago' => 'πριν από :time',
'from_now' => 'σε :time από τώρα',
'after' => ':time μετά',
Expand Down
Loading

0 comments on commit bab61b2

Please sign in to comment.