Plural in time_ago

Asked

Viewed 42 times

0

I’m having trouble 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>

inserir a descrição da imagem aqui

A simple solution would be to just remove the "s" from the $periods[$val] .= "s";, only it would be something half mouth as "5 months ago", the right is to leave "5 months ago".

2 answers

1


Just add a few more conditions, check also the value of $val, it is 5 then add "ese" if not, add "ês", note that in the plural month there is no rise, in the array I left only the "m", and then add the rest via 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 >
  • Managed to solve one, but another kkkk https://i.imgur.com/5SSVWZx.png appears

  • I’m almost giving up, I’ve searched all over google and I can’t find any solution.

  • edited the answer see if solved

  • just forehead, it worked perfectly. thank you very much, I spent all day cracking head with it :)

0

I saw that already solved, but in my opinion I would prefer to use the ternary, because the only case where it is not necessary the concatenation with the letter "s", is specifically in the word month and only this, so I will leave the 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);

  • 1

    really worked perfectly, thanks for sharing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.