Answers for "php get time ago function"

PHP
3

function return time ago php

public function convert_time_to_days($date) {
       $current_date = date("Y-m-d H:m:s");
       $time = array();
       $day = floor((strtotime($current_date) - strtotime($date)) / (60 * 60 * 24));
 
       if ($day == 0) {
           $hour = floor((strtotime($current_date) - strtotime($date)) / (60 * 60));
 
           if ($hour == 0) {
               $minute = floor((strtotime($current_date) - strtotime($date)) / (60));
               $time = $minute . __d('course', "minutes ago");
 
           } else {
               $time = $hour . __d('course', "hours ago");
           }
      
       } else {
           $time = $day . __d('course', "days ago");
       }
      
       return $time;
   }
Posted by: Guest on October-06-2020
1

time ago php

<!DOCTYPE html>
<html>
<body>

<?php

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

echo time_elapsed_string(date("Y-m-d H:i:s", 1621173863));
?>

</body>
</html>

  
 echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);


Output :

4 months ago
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Posted by: Guest on May-16-2021

Browse Popular Code Answers by Language