0
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 fix from "month" to "months" by code, but I have to keep those codes because of "second", "minute", "year", etc.:
if($difference != 1)
{
$periods[$j].= "s";
}
but in this code, it appeared as second, minute, ...
– Gustavo Reis