Jacob Emerick blogged about “Fuzzy Timestamps” a while back. In his post he points out the differences between GitHub’s and Twitter’s implementation of fuzzy dates. Fuzzy dates are pretty common in social media and blogs. Instead of displaying an ugly timestamp, like “Posted: June 10th, 2014 21:16“, fuzzy dates are round numbers which are easier to read. For example, “Posted: 8 minutes ago“.
Sadly, Jacob didn’t include a code example of how he ended up implementing his livestream. So, I’ve come up with my own. This is my own mix of messages (seems to work well with the application it’s built for).
Ha, no, I didn’t include a code example in that one. Complicated logic like that I try to keep as trade secrets 😉
Did want to point out something with your implementation, though. The only way you’ll get a valid response is by physically calling the __construct() method on an instantiated class, which may look a little weird. If you just use new FuzzyDate($variable) you will not get the expected value – the constructor (when called as a constructor) always returns void().
If you wanted to get tricky, replace the __construct with __invoke. Then you could do
$fuzzy = new Fuzzydate();