How to convert timestamp to time ago format in PHP

Time Ago format is used to display time in human-readable format, such as 5 seconds ago, 2 minutes ago, 1 day ago, etc. Relative time format helps to avoid different time zone conversions. In this example code snippet, we will show you how to create Time Ago function to convert timestamp to time ago using PHP.

We will create a custom function to convert timestamp to time ago in PHP. This function will help you to convert DateTime into relative time format easily.

The timeAgo() function is used to get the DateTime in time ago format. This function takes two parameters as input, $time (required) and $short (optional).

  • $time – Timestamp. The program will subtract it from the current time, and convert it into the time ago format.
  • $short – true/false. Specify whether you want to display short text format (1m ago, 1h ago, etc).
function timeAgo($time$short false){ 
    
$SECOND 1;
    
$MINUTE 60 $SECOND;
    
$HOUR 60 $MINUTE;
    
$DAY 24 $HOUR;
    
$MONTH 30 $DAY;
    
$before time() - $time;

    if(
$before 0){
        return 
"not yet";
    }

    if(
$short){
        if(
$before $MINUTE){
            return (
$before <5) ? "just now" $before " ago";
        }

        if(
$before $MINUTE){
            return 
"1m ago";
        }

        if(
$before 45 $MINUTE){
            return 
floor($before 60) . "m ago";
        }

        if(
$before 90 $MINUTE){
            return 
"1h ago";
        }

        if(
$before 24 $HOUR){
            return 
floor($before 60 60). "h ago";
        }

        if(
$before 48 $HOUR){
            return 
"1d ago";
        }

        if(
$before 30 $DAY){
            return 
floor($before 60 60 24) . "d ago";
        }

        if(
$before 12 $MONTH){
            
$months floor($before 60 60 24 30);
            return 
$months <= "1mo ago" $months "mo ago";
        }else{
            
$years floor  ($before 60 60 24 30 12);
            return 
$years <= "1y ago" $years."y ago";
        }
    }

    if(
$before $MINUTE){
        return (
$before <= 1) ? "just now" $before " seconds ago";
    }

    if(
$before $MINUTE){
        return 
"a minute ago";
    }

    if(
$before 45 $MINUTE){
        return 
floor($before 60) . " minutes ago";
    }

    if(
$before 90 $MINUTE){
        return 
"an hour ago";
    }

    if(
$before 24 $HOUR){
        return (
floor($before 60 60) == 'about an hour' floor($before 60 60).' hours'). " ago";
    }

    if(
$before 48 $HOUR){
        return 
"yesterday";
    }

    if(
$before 30 $DAY){
        return 
floor($before 60 60 24) . " days ago";
    }

    if(
$before 12 $MONTH){
        
$months floor($before 60 60 24 30);
        return 
$months <= "one month ago" $months " months ago";
    }else{
        
$years floor  ($before 60 60 24 30 12);
        return 
$years <= "one year ago" $years." years ago";
    }

    return 
"$time";
}

timeAgo() function usage:
You need to pass the timestamp in timeAgo() function. If you have DateTime format, convert it to the timestamp using strtotime() function in PHP and pass it to the timeAgo() function.

$date_time '2024-06-17 14:30:45'; 
$timestamp strtotime($date_time);

echo 
timeAgo($timestamp);

Leave a reply

keyboard_double_arrow_up