Plural on time_ago

0

I'm having problem in determining the plural in the time display.

Function:

/**
 *	@ http://us.php.net/manual/en/function.time.php#71342
 */
function time_ago($timestamp, $recursive = 0)
{
	$current_time = time();
	$difference = $current_time - $timestamp;
	$periods = array("segundo", "minuto", "hora", "dia", "semana", "mês", "ano", "década");
	$lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
	for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
	if ($val < 0) $val = 0;
	$new_time = $current_time - ($difference % $lengths[$val]);
	$number = floor($number);
	if($number != 1)
	{
		$periods[$val] .= "s";
	}
	$text = sprintf("%d %s ", $number, $periods[$val]);   
	
	if (($recursive == 1) && ($val >= 1) && (($current_time - $new_time) > 0))
	{
		$text .= time_ago($new_time);
	}
	return $text;
}
<span class="time"><?=time_ago($item['mtime'])?> atrás</span>

Asimplesolutionwouldbetojustremovethe"s" from $periods[$val] .= "s"; , but it would be something half a mouth like "5 months ago", the right thing is to leave "5 months ago".

    
asked by anonymous 01.05.2018 / 17:32

2 answers

1

Just add some more conditions, check also the value of $val , it is 5 then add "eses" if not, add "ês", note that in the month month there is no ascending, m ", and then added the remainder through the if

<?php
function time_ago($timestamp, $recursive = 0)
{
    $current_time = time();
    $difference = $current_time - $timestamp;
    $periods = array("segundo", "minuto", "hora", "dia", "semana", "m", "ano", "década");
    $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
    for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
    if ($val < 0) $val = 0;
    $new_time = $current_time - ($difference % $lengths[$val]);
    $number = floor($number);
    if($number == 1 && $val == 5)
    {
        $periods[$val] .= "ês";
    }
    else if($number != 1 && $val == 5)
    {
        $periods[$val] .= "eses";
    }
    else if($number != 1)
    {
        $periods[$val] .= "s";
    }
    $text = sprintf("%d %s ", $number, $periods[$val]);   

    if (($recursive == 1) && ($val >= 1) && (($current_time - $new_time) > 0))
    {
        $text .= time_ago($new_time);
    }
    return $text;
}
?>

<span class="time"><?=time_ago(time() -  (40 * 24 * 60 * 60))?> atrás</span> <! Mostra 1 mês atrás >
<span class="time"><?=time_ago(time() -  (70 * 24 * 60 * 60))?> atrás</span> <! Mostra 2 meses atrás >
    
01.05.2018 / 20:59
0

I saw that it already solved, but in my opinion I would prefer to use the ternary, since the only case in which it is not necessary to concatenate with the letter "s", is specifically the word month and only this, answer here:

    /**
     *  @ http://us.php.net/manual/en/function.time.php#71342
     */
    function time_ago($timestamp, $recursive = 0)
    {
        $current_time = time();
        $difference = $current_time - $timestamp;
        $periods = array("segundo", "minuto", "hora", "dia", "semana", "mês", "ano", "década");
        $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
        for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
        if ($val < 0) $val = 0;
        $new_time = $current_time - ($difference % $lengths[$val]);
        $number = floor($number);
        if($number != 1)
        {
            $periods[$val] = ($periods[$val] === 'mês' ? "meses" : $periods[$val] . "s");
        }
        $text = sprintf("%d %s ", $number, $periods[$val]);   

        if (($recursive == 1) && ($val >= 1) && (($current_time - $new_time) > 0))
        {
            $text .= time_ago($new_time);
        }
        return $text;
    }

    $teste = time_ago(1519862400);
    print($teste);
    
01.05.2018 / 21:43