ajax loader

Esqueceu sua senha?

Formatar datetime para tempo por extenso igual sites como youtube
Olá pessoal, algum tempo atrás eu precisei criar uma função para formatar o datetime do banco para ficar por extenso, similar a sites como youtube etc.

Estou aqui contribuindo com a função para aqueles interessados em utilizar :P

<?php 

	function niceTime($input) {
	$minute = 60; // seconds
        $hour = 3600; // seconds
        $day = 86400; // seconds
        $week = 604800; // seconds
        $month = 2629743; // seconds
        $year = 31556926; // seconds
        
		$localtime = time();
		$time = strtotime($input); // transform input to timestamp
        $diff = $localtime - $time; // get difference in seconds
        
        switch (true) {
                     
            // seconds
            case ($diff < $minute); // if difference less them a minute
            $count = $diff; // hold the value
            if ($count == 0) {
                $count = 1;
                $suffix = "segundo";
            } elseif ($count == 1) {
                $suffix = "segundo";
            } else {
                $suffix = "segundos";
            }
            break;
            
            // minute
            case ($diff > $minute && $diff < $hour); /* if difference greater them a minute and if diff less them an hour */
            $count = floor($diff / $minute); // here i divided by minute in order to get 1
            if ($count == 1) {
                $suffix = "minuto";
            } else {
                $suffix = "minutos";
            }
            break;

            // hour
            case ($diff > $hour && $diff < $day); /* if difference greater them an hour and if diff less them a day */
            $count = floor($diff / $hour); // here i divided by hour in order to get 1
            if ($count == 1) {
                $suffix = "hora";
            } else {
                $suffix = "horas";
            }
            break;
            
            // day
            case ($diff > $day && $diff < $week); /* if difference greater them a day and if diff less them a week */
            $count = floor($diff / $day); // here i divided by day in order to get 1
            if ($count == 1) {
                $suffix = "dia";
            } else {
                $suffix = "dias";
            }
            break;

            // week
            case ($diff > $week && $diff < $month); /* if difference greater them a week and if diff less them a month */
            $count = floor($diff / $week); // here i divided by week in order to get 1
            if ($count == 1) {
                $suffix = "semana";
            } else {
                $suffix = "semanas";
            }
            break;
            
            // month
            case ($diff > $month && $diff < $year); /* if difference greater them a month and if diff less them a year */
            $count = floor($diff / $month); // here i divided by month in order to get 1
            if ($count == 1) {
                $suffix = "mês";
            } else {
                $suffix = "mêses";
            }
            break;

            // year
            case ($diff > $year); /* if difference greater them a year */
            $count = floor($diff / $year); // here i divided by year in order to get 1
            if ($count == 1) {
                $suffix = "ano";
            } else {
                $suffix = "anos";
            }
            break;
        }
		return "Postado " . $count . " " . $suffix . " atrás";
	}

 echo niceTime("2012-07-24 11:10"); // chamar função passando o datetime

?>
 
24/07/2012 às 19:11    acessos (5860)     rodr1go

Gostaria de deixar o seu comentário? Para deixar o seu comentário por favor faça o registro em nosso site.   Registrar