PHP was implemented just a few minutes ago, a few hours ago, a few days ago

Time:2021-8-6
<?php
/*
*@ param $posttime: 2020-06-3 12:00:18
*/

function time_ago($posttime){
     //Timestamp of the current time
    $nowtimes = strtotime(date('Y-m-d H:i:s'),time());
    //Timestamp of the previous time parameter
    $posttimes = strtotime($posttime);
    //Phase difference timestamp
    $counttime = $nowtimes - $posttimes;
    
    //Time conversion
    if($counttime<=10){
    
       Return 'just now';
        
    }else if($counttime>10 && $counttime<=30){
    
       Return 'just now';
        
    }else if($counttime>30 && $counttime<=60){
    
        Return 'just a moment';
        
    }else if($counttime>60 && $counttime<=120){
    
       Return '1 minute ago';
        
    }else if($counttime>120 && $counttime<=180){
    
       Return '2 minutes ago';
        
    }else if($counttime>180 && $counttime<3600){
    
       Return intval (($counttime / 60)). 'minutes ago';
        
    }else if($counttime>=3600 && $counttime<3600*24){
    
       Return intval (($counttime / 3600)). 'hours ago';
        
    }else if($counttime>=3600*24 && $counttime<3600*24*2){
    
       Return 'yesterday';
        
    }else if($counttime>=3600*24*2 && $counttime<3600*24*3){
    
       Return 'the day before yesterday';
        
    }else if($counttime>=3600*24*3 && $counttime<=3600*24*20){
    
       Return intval (($counttime / (3600 * 24))). 'days ago';
        
    }else{
    
       return $posttime;
        
    }
}

//Call
$posttime_1 = "2020-05-31 05:00:18";
$posttime_2 = "2020-06-2 11:00:00";
$posttime_3 = "2020-06-3 12:05:18";
$date_1 = time_ago($posttime_1);
$date_2 = time_ago($posttime_2);
$date_3 = time_ago($posttime_3);

echo "<h1 style=\"text-align:center;margin-top:50px;\">".$date_1."</h1>";
echo "<h1 style=\"text-align:center;margin-top:50px;\">".$date_2."</h1>";
echo "<h1 style=\"text-align:center;margin-top:50px;\">".$date_3."</h1>";
?>

PHP was implemented just a few minutes ago, a few hours ago, a few days ago

Author:TANKING
Web:https://www.likeyunba.com/
Date:2020-06-03
WeChat:face6009