Time Ago Function in PHP

I this article we will provide complete code with demo for convert given date time into timestamp using php and show time-ago format. we have create custom date_time_ago() function, in this function need 3 parameters first is your date time string, second one is date time format and third is time format.

$input_date – this is your date time string
$date_format – this is date time complete format, if time ago grater then 2 days then date show in this given format.
$time_format – this is time format when show time, like Yesterday at 6:45 PM

In date_time_ago() function we convert given date into timestamp using php strtotime() function  and get current timestamp using php default time() function and subtracts original timestamp from current timestamp and calculate difference.

on the basis of time difference we calculate time-ago string and return time-ago string. here is the complete code :

date_time_ago() function declaration :

Function calling and print output :