How to convert timestamp to time ago in PHP?

Spread the love

Sometimes, we want to convert timestamp to time ago in PHP.

In this article, we’ll look at how to convert timestamp to time ago in PHP.

How to convert timestamp to time ago in PHP?

To convert timestamp to time ago in PHP, we can use Carbon.

For instance, we write

require 'vendor/autoload.php';

use Carbon\Carbon;

$dt = Carbon::parse('2019-9-5 23:26:11.123789');
echo $dt->diffForHumans();

to call Carbon::parse with the time string that we want to get the elapsed datetime string in a human readable format.

Then we call diffForHumans to return a string of the elasped datetime in human readable format.

We install it with

composer require nesbot/carbon

Conclusion

To convert timestamp to time ago in PHP, we can use Carbon.

Leave a Reply

Your email address will not be published.