How to convert "month" to "months", using timestamp in PHP?
-
Here's 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}";
}
In HTML:
<li class="published-date"><?php echo longadata("2017-08-28") ?></li>
In the result:
3 MÊSS ATRÁS
I want to correct "month" for "months" by code, but I have to keep these codes because of "second", "minute", "year", etc.:
if($difference != 1)
{
$periods[$j].= "s";
}
-
From what I understand the answer seems to be too obvious, but I don't think that's it, good in any case... In that part I think you just need to put an "e":
if($difference != 1) { $periods[$j].= "es"; }
the exit will be:
3 _meses _atras
I don't know if that was the problem, but if you don't try to explain better that I can help you!