Difference for Humans
It is easier for humans to read 1 month ago compared to 30 days ago. This is a common function seen in most date libraries so I thought I would add it here as well. The lone argument for the function is the other Carbon instance to diff against, and of course it defaults to now() if not specified.
This method will add a phrase after the difference value relative to the instance and the passed in instance. There are 4 possibilities:
- When comparing a value in the past to default now:
- 1 hour ago
- 5 months ago
- When comparing a value in the future to default now:
- 1 hour from now
- 5 months from now
- When comparing a value in the past to another value:
- 1 hour before
- 5 months before
- When comparing a value in the future to another value:
- 1 hour after
- 5 months after
You may also pass CarbonInterface::DIFF_ABSOLUTE as a 2nd parameter to remove the modifiers ago, from now, etc : diffForHumans($other, CarbonInterface::DIFF_ABSOLUTE), CarbonInterface::DIFF_RELATIVE_TO_NOW to get modifiers ago or from now, CarbonInterface::DIFF_RELATIVE_TO_OTHER to get the modifiers before or after or CarbonInterface::DIFF_RELATIVE_AUTO (default mode) to get the modifiers either ago/from now if the 2 second argument is null or before/after if not.
You may pass true as a 3rd parameter to use short syntax if available in the locale used : diffForHumans($other, CarbonInterface::DIFF_RELATIVE_AUTO, true).
You may pass a number between 1 and 6 as a 4th parameter to get the difference in multiple parts (more precise diff) : diffForHumans($other, CarbonInterface::DIFF_RELATIVE_AUTO, false, 4).
The $other instance can be a DateTime, a Carbon instance or any object that implement DateTimeInterface, if a string is passed it will be parsed to get a Carbon instance and if null is passed, Carbon::now() will be used instead.
To avoid having too much argument and mix the order, you can use the verbose methods:
shortAbsoluteDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)longAbsoluteDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)shortRelativeDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)longRelativeDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)shortRelativeToNowDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)longRelativeToNowDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)shortRelativeToOtherDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)longRelativeToOtherDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)
PS: $other and $parts arguments can be swapped as need.
// The most typical usage is for comments
// The instance is the date the comment was created and its being compared to default now()
echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago
echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 11 months after
$dt = Carbon::createFromDate(2011, 8, 1);
echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 month before
echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after
echo Carbon::now()->addSeconds(5)->diffForHumans(); // 4 seconds from now
echo Carbon::now()->subDays(24)->diffForHumans(); // 3 weeks ago
echo Carbon::now()->subDays(24)->longAbsoluteDiffForHumans(); // 3 weeks
echo Carbon::parse('2019-08-03')->diffForHumans('2019-08-13'); // 1 week before
echo Carbon::parse('2000-01-01 00:50:32')->diffForHumans('@946684800'); // 50 minutes after
echo Carbon::create(2018, 2, 26, 4, 29, 43)->longRelativeDiffForHumans(Carbon::create(2016, 6, 21, 0, 0, 0), 6); // 1 year 8 months 5 days 4 hours 29 minutes 43 seconds afterYou can also change the locale of the string using $date->locale('fr') before the diffForHumans() call. See the localization section for more detail.
Since 2.9.0 diffForHumans() parameters can be passed as an array:
echo Carbon::now()->diffForHumans(['options' => 0]); // 0 seconds ago
echo "\n";
// default options:
echo Carbon::now()->diffForHumans(['options' => Carbon::NO_ZERO_DIFF]); // 1 second ago
echo "\n";
echo Carbon::now()->diffForHumans(['options' => Carbon::JUST_NOW]); // just now
echo "\n";
echo Carbon::now()->subDay()->diffForHumans(['options' => 0]); // 1 day ago
echo "\n";
echo Carbon::now()->subDay()->diffForHumans(['options' => Carbon::ONE_DAY_WORDS]); // yesterday
echo "\n";
echo Carbon::now()->subDays(2)->diffForHumans(['options' => 0]); // 2 days ago
echo "\n";
echo Carbon::now()->subDays(2)->diffForHumans(['options' => Carbon::TWO_DAY_WORDS]); // before yesterday
echo "\n";
// Options can be combined with pipes
$now = Carbon::now();
echo $now->diffForHumans(['options' => Carbon::JUST_NOW | Carbon::ONE_DAY_WORDS | Carbon::TWO_DAY_WORDS]); // just now
echo "\n";
// Reference date for differences is `now` but you can use any other date (string, DateTime or Carbon instance):
$yesterday = $now->copy()->subDay();
echo $now->diffForHumans($yesterday); // 1 day after
echo "\n";
// By default differences methods produce "ago"/"from now" syntax using default reference (now),
// and "after"/"before" with other references
// But you can customize the syntax:
echo $now->diffForHumans($yesterday, ['syntax' => CarbonInterface::DIFF_RELATIVE_TO_NOW]); // 1 day from now
echo "\n";
echo $now->diffForHumans($yesterday, ['syntax' => 0]); // 1 day after
echo "\n";
echo $yesterday->diffForHumans(['syntax' => CarbonInterface::DIFF_ABSOLUTE]); // 1 day
echo "\n";
// Combined with options:
echo $now->diffForHumans($yesterday, [
'syntax' => CarbonInterface::DIFF_RELATIVE_TO_NOW,
'options' => Carbon::JUST_NOW | Carbon::ONE_DAY_WORDS | Carbon::TWO_DAY_WORDS,
]); // tomorrow
echo "\n";
// Other parameters:
echo $now->copy()->subHours(5)->subMinutes(30)->subSeconds(10)->diffForHumans([
'parts' => 2,
]); // 5 hours 30 minutes ago
echo "\n";
echo $now->copy()->subHours(5)->subMinutes(30)->subSeconds(10)->diffForHumans([
'parts' => 3, // Use -1 or INF for no limit
]); // 5 hours 30 minutes 10 seconds ago
echo "\n";
echo $now->copy()->subHours(5)->subMinutes(30)->subSeconds(10)->diffForHumans([
'parts' => 3,
'join' => ', ', // join with commas
]); // 5 hours, 30 minutes, 10 seconds ago
echo "\n";
echo $now->copy()->subHours(5)->subMinutes(30)->subSeconds(10)->diffForHumans([
'parts' => 3,
'join' => true, // join with natural syntax as per current locale
]); // 5 hours, 30 minutes and 10 seconds ago
echo "\n";
echo $now->copy()->subHours(5)->subMinutes(30)->subSeconds(10)->locale('fr')->diffForHumans([
'parts' => 3,
'join' => true, // join with natural syntax as per current locale
]); // il y a 5 heures, 30 minutes et 10 secondes
echo "\n";
echo $now->copy()->subHours(5)->subMinutes(30)->subSeconds(10)->diffForHumans([
'parts' => 3,
'short' => true, // short syntax as per current locale
]); // 5h 30m 10s ago
// 'aUnit' option added in 2.13.0 allows you to prefer "a day", "an hour", etc. over "1 day", "1 hour"
// for singular values when it's available in the current locale
echo $now->copy()->subHour()->diffForHumans([
'aUnit' => true,
]); // an hour ago
// Before 2.9.0, you need to pass parameters as ordered parameters:
// ->diffForHumans($other, $syntax, $short, $parts, $options)
// and 'join' was not supportedIf the argument is omitted or set to null, only Carbon::NO_ZERO_DIFF is enabled. Available options are:
'altNumbers' | Use alternative numbers if available (from the current language if
|
'aUnit' | When |
'join' | Determines how to join multiple parts of the string
|
'locale' | Language in which the diff should be output (has no effect if 'translator' key is set) |
'minimumUnit' | Determines the smallest unit of time to display. Default value: 's'.
|
'options' | If the Note: use the pipe operator to enable/disable multiple option at once: Available options are:
You also can use |
'other' | Date and Time Comparison reference:
|
'parts' | Maximum number of parts to display (default value:
|
'short' | If |
'skip' | List of units to skip (array of strings or a single string). It can be the unit name (singular or plural) or its shortcut (y, m, w, d, h, min, s, ms, µs). |
'syntax' | Text modifiers appended to the period representation like "from now", "ago", "before" etc. Possible values:
|
'translator' | A custom translator to use to translate the output. |
Aliases and reverse methods are provided for semantic purpose:
from($other = null, $syntax = null, $short = false, $parts = 1, $options = null)(alias of diffForHumans)since($other = null, $syntax = null, $short = false, $parts = 1, $options = null)(alias of diffForHumans)to($other = null, $syntax = null, $short = false, $parts = 1, $options = null)(inverse result, swap before and future diff)until($other = null, $syntax = null, $short = false, $parts = 1, $options = null)(alias of to)fromNow($syntax = null, $short = false, $parts = 1, $options = null)(alias of from with first argument omitted unless the first argument is aDateTimeInterface, now used instead), for semantic usage: produce an "3 hours from now"-like string with dates in the futureago($syntax = null, $short = false, $parts = 1, $options = null)(alias of fromNow), for semantic usage: produce an "3 hours ago"-like string with dates in the pasttoNow($syntax = null, $short = false, $parts = 1, $options = null)(alias of to with first argument omitted, now used instead)timespan($other = null, $timezone = null)calls diffForHumans with optionsjoin = ', '(coma-separated),syntax = CarbonInterface::DIFF_ABSOLUTE(no "ago"/"from now"/"before"/"after" wording),options = CarbonInterface::NO_ZERO_DIFF(no "just now"/"yesterday"/"tomorrow" wording),parts = -1(no limits) In this mode, you can't change options but you can pass an optional date to compare with or a string + timezone to parse to get this date.
