Función Time Ago Php

funcion time ago o como mostrar la fecha tipo Hace un momento , hace un segundo, Etc

function timeago($datetime, $full = false) {

 

 global $txt;

 

    $date = '@'.$datetime;

    $now = new DateTime;

    $ago = new DateTime($date);

    $diff = $now->diff($ago);

 

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

    $diff->d -= $diff->w * 7;

 

    $string = array(

        'y' => $txt['bs_year'],

        'm' => $txt['bs_month'],

        'w' => $txt['bs_week'],

        'd' => $txt['bs_day'],

        'h' => $txt['bs_hour'],

        'i' => $txt['bs_minute'],

        's' => $txt['bs_second'],

    );

    foreach ($string as $k => &$v) {

        if ($diff->$k) {

            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? ($k=='m'? $txt['bs_es']:'s') : '');

        } else {

            unset($string[$k]);

        }

    }

 

    if (!$full) $string = array_slice($string, 0, 1);

    return $string ?  ''.$txt['bs_Hace'].' '.implode(', ', $string)  : $txt['bs_momento'];

}

variables de texto

//time ago

$txt['bs_year'] = 'Año';

$txt['bs_month'] = 'Mes';

$txt['bs_week'] = 'Semana';

$txt['bs_day'] = 'Día';

$txt['bs_hour'] = 'Hora';

$txt['bs_minute'] = 'Minuto';

$txt['bs_second'] = 'Segundo';

$txt['bs_Hace'] = 'Hace';

$txt['bs_momento'] = 'Hace un Momento';

$txt['bs_es'] ='es';

pero como lo usas?
$time tiene que tener el formato timestamp

$time = '1313545645';

timeago($time)
Scroll to Top