Why does diffForHumans say 1 day ago for something 47 hours old?
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.
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.
If you want more precision, ask for more parts.
And when space is tight, short and join keep the extra precision compact.
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.
Toss a coin in the jar if you found this helpful.
You received this email because you signed up at masteringlaravel.io. Thanks!
Want fewer emails? Click here to adjust the email frequency.
Or click here to update your email address.