How to convert "month" to "months" using timestamp in PHP?

0

Here is the code:

function longadata($data)
{
    if(empty($data)) 
    {
        return "No date provided";
    }

    $periods         = array(_segundo, _minuto, _hora, _dia, _semana, _mes, _ano, _decada);
    $lengths         = array("60", "60", "24", "7", "4.35", "12", "10");

    $now             = time();
    $unix_date       = strtotime($data);

    if (empty($unix_date)) 
    {    
        return "Bad date";
    }

    if ($now > $unix_date) 
    {    
        $difference     = $now - $unix_date;
        $tense          = _atras;   
    } 

    else 
    {
        $difference     = $unix_date - $now;
        $tense          = _agora;
    }

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) 
    {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    if($difference != 1) 
    {
        $periods[$j].= "s";
    }

    return "$difference $periods[$j] {$tense}";
}

No HTML:

<li class="published-date"><?php echo longadata("2017-08-28") ?></li>

No result:

3 MÊSS ATRÁS

I want to correct from "month" to "months" by the code, but I have to keep these codes because of "second", "minute", "year"

if($difference != 1) 
 {
   $periods[$j].= "s";
 }
    
asked by anonymous 16.11.2017 / 09:32

2 answers

0

You can check if the last letter is s or if $j is 5 :

if($difference !== 1){
    $periods[$j] .= mb_substr($periods[$j], -1, 1) === 's' ? 'es' : 's';
}

The other way would be:

if($difference !== 1){
    $periods[$j] .= $j === 5 ? 'es' : 's';
}

If there is an accent in "month" this will have to be removed, you can simply remove all accents using strtr , for example:

if($difference !== 1){
    $periods[$j] .= $j === 5 ? 'es' : 's';
    $periods[$j] = str_replace('ê', 'e', $periods[$j]);
}
    
16.11.2017 / 11:59
0

From what I understand the answer seems to me to be too obvious, but I do not think it's that good in any case ... In that part I think you just need to put an "e":

if($difference != 1) 
 {
   $periods[$j].= "es";
 }

the output will be:

3 _meses _atras

I do not know if that was the problem, but if not try explaining better that I will be able to help you!

    
16.11.2017 / 11:07