1

How can format time in twig like this: e.g. "13h23" (Hours: 13, minutes: 23) and "13u23" (Hours: 13, minutes: 23)

if I use twig "date" filter, like this: date|date("Hhi") the "h" is converted to 12-hour and I want to include the "h" letter on it. Something similar happens with the other case.

  • You can check this: http://stackoverflow.com/questions/27838350/how-do-i-format-time-to-hhmm-using-twig-symfony – gsone Mar 20 '17 at 22:43
  • 2
    Possible duplicate of [How to render a DateTime object in a Twig template](http://stackoverflow.com/questions/8318914/how-to-render-a-datetime-object-in-a-twig-template) – Blackbam Mar 20 '17 at 23:01

1 Answers1

3

Everyone who read this question and answered or commented didn't actually understand the question. The OP wants to print 'h' or 'u' between the hours and minutes.

You can do that by using double backslash in front of any character you want to escape. So like so for your example:

{{ created_date|date('H\\hi') }}

or

{{ created_date|date('H\\ui') }}

Here is a twigfiddle example: https://twigfiddle.com/ypnau1

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • 1
    To add some context here: Internally the Twig `date` filter uses PHP's mechanisms to format the date. The way to escape a format character to let it not be interpreted is described in http://php.net/manual/en/function.date.php#example-2549. – xabbuh Mar 21 '17 at 09:27
  • Thank you so much!, that was what I meant! – EJC Software solutions Mar 21 '17 at 19:35
  • @xabbuh in the documentation, it's specified you must use one backslash, not two. The behaviour here is different. Twig doc specifiying the double backslash need: https://twig.symfony.com/doc/2.x/filters/date.html – Laurent W. Oct 17 '18 at 08:48
  • You are right. The two backslashes are needed so that Twig does not interpret it as an escape sequence. PHP's `date()` function will then be invoked with one backslash. – xabbuh Oct 19 '18 at 08:48