O
O
ol sm2017-08-30 21:02:34
symfony
ol sm, 2017-08-30 21:02:34

Has anyone found a filter for twig with countdown date?

Good evening!
Has anyone found a filter for the number of n days / month / year on the site?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Talalaev, 2017-08-30
@neuotq

The Twig-extentions package has a time_diff filter (a DateTime::diff() wrapper from php) that can be used for similar functionality.

O
ol sm, 2017-08-30
@ol_efk

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;
    }

And since months and a year it has not worked, who will supplement?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question