11

Twig documentation describes how to set the default date format for the date filter:

$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');

How can do this setting globally in Symfony2?

Philipp Rieber
  • 1,531
  • 17
  • 25

6 Answers6

20

For a more detailed solution.

in your bundle create a Services folder that can contain the event listener

namespace MyApp\AppBundle\Services;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class TwigDateRequestListener
{
    protected $twig;

    function __construct(\Twig_Environment $twig) {
        $this->twig = $twig;
    }

    public function onKernelRequest(GetResponseEvent $event) {
        $this->twig->getExtension('core')->setDateFormat('Y-m-d', '%d days');
    }
}

Then we will want symfony to find this listener. In the Resources/config/services.yml file put

services:
    twigdate.listener.request:
        class: MyApp\AppBundle\Services\TwigDateRequestListener
        arguments: [@twig]
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

by specifying @twig as an argument it will be injected into the TwigDateRequestListener

Make shure that your importing the services.yml at the top of app/config.yml

imports:
    - { resource: @MyAppAppBundle/Resources/config/services.yml }

Now you should be able to skip the format in the date filter as such

{{ myentity.dateAdded|date }}

and it should get the formatting from the service.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
Leon Radley
  • 7,596
  • 5
  • 35
  • 54
14

As of Symfony 2.7, you can configure the default date format globally in config.yml:

# app/config/config.yml
twig:
    date:
        format: d.m.Y, H:i:s
        interval_format: '%%d days'
        timezone: Europe/Paris

The same is also possible for the number_format filter. Details can be found here: http://symfony.com/blog/new-in-symfony-2-7-default-date-and-number-format-configuration

Philipp Rieber
  • 1,531
  • 17
  • 25
  • Sadly, there is no way to distinguish date from datetime objects, and thus it is not possible to set different formats for date and datetime, AFAIK... – Vincent Pazeller Aug 25 '17 at 11:59
2

In controller you can do

$this->get('twig')->getExtension('core')->setDateFormat('d/m/Y', '%d days');
Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
  • 1
    This was my first idea, too. I would prefer a global setting in config.yml or with a listener so that it's valid in the whole project. I just added "globally" to my question to be clear. – Philipp Rieber Mar 27 '12 at 13:36
  • Well it can not be done by adding a config in config.yml AFIK. TwigBundle does not expose core extension. Yes you can add a request listener to accomplish this. – Mun Mun Das Mar 27 '12 at 17:27
  • Setting works fine globally when done in a request listener. I'm injecting the `twig` service of class `\Twig_Environment` and can do the setting as described above. – Philipp Rieber Mar 30 '12 at 16:02
  • Can you supply an example of how to handle a default date format via the config.yml – Rvanlaak Feb 25 '13 at 13:14
1

for Symfony 4 you can do this for those that need.

twig:
    ...
    date:
        format: c
        timezone: UTC
    ....
Chris
  • 811
  • 8
  • 17
0

At least in my Twig installation (frameworkless) no extension named 'core' exists, I had to use Twig_Extension_Core

$twig->getExtension('Twig_Extension_Core')->setDateFormat($dateFormat);

Tested in Twig version v2.14.6

-1

The global Twig configuration options can be found on:

http://symfony.com/doc/2.0/reference/configuration/twig.html

In my opinion the 'date_format' option should be added here, since making use of the Sonata Intl bundle is overkill for most of the users.

Rvanlaak
  • 2,971
  • 20
  • 40