Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
The Twig-extentions package has a time_diff filter (a DateTime::diff() wrapper from php) that can be used for similar functionality.
public function createdAgo(\DateTime $dateTime)
{
$delta = time() - $dateTime->getTimestamp();
if ($delta < 0)
throw new \InvalidArgumentException("createdAgo is unable to handle dates in the future");
$duration = "";
if ($delta < 60)
{
$time = $delta;
$duration = $time . " секунд" . (($time > 1) ? "ы" : "") . " назад";
}
else if ($delta <= 3600)
{
$time = floor($delta / 60);
$duration = $time . " минут" . (($time > 1) ? "ы" : "") . " назад";
}
else if ($delta <= 86400)
{
$time = floor($delta / 3600);
$duration = $time . " час" . (($time > 1) ? "ов" : "") . " назад";
}
else
{
$time = floor($delta / 86400);
$duration = $time . " дн" . (($time > 1) ? "ей" : "") . " назад";
}
return $duration;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question