Carbon's diffForHumans() is a great way to show relative times.
"2 hours ago" is a lot friendlier than a full timestamp.
But it has a quirk you might not expect.
now()->subHours(47)->diffForHumans();
// 1 day ago
now()->subHours(2)->subMinutes(59)->diffForHumans();
// 2 hours ago
By default, it only shows the largest unit, and it drops whatever is left over. 47 hours is 1 day and 23 hours, so you get "1 day ago." It truncates instead of rounding.
For a comment timestamp, that's probably fine. For something like "last backup" or "time since the last failed job," it can be misleading.
If you want rounding, there's an option for that.
use Carbon\CarbonInterface;
now()->subHours(47)->diffForHumans(['options' => CarbonInterface::ROUND]);
// 2 days ago
If you want more precision, ask for more parts.
now()->subHours(47)->diffForHumans(['parts' => 2]);
// 1 day 23 hours ago
And when space is tight, short and join keep the extra precision compact.
now()->subHours(2)->subMinutes(30)->diffForHumans([
'parts' => 2,
'short' => true,
'join' => ' ',
]);
// 2h 30m ago
The default is a reasonable choice for casual timestamps. Just make sure it's a choice, and not something you found out about when the numbers didn't add up.
Here to help,
Joel
P.S. Small details like this are easy to get wrong without noticing. A quick test with a frozen clock catches them, and our testing workshop shows you how.