Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set timezone in symfony 3?

I have a symfony 3 application deployed in Ubuntu 14.04 server.

When I execute "date" in the command window, the server response me "Fri May 11 10:02:30 CEST 2018" but when I dump a datetime in twig as "{{ dump("now"|date) }}", the response is "May 11, 2018 04:04".

If I put in the header of the controller function (route), "date_default_timezone_set("Europe/Madrid");" it works fine, but I don't want to copy this line in all functions of all controllers.

Then, I want to know where can I set the Timezone as configuration of Symfony or where can I set this PHP command (date_default_timezone_set("Europe/Madrid")) globally for all the controllers.

Thank you

[EDIT]

One solution may be put this code in the header of the controller, but I want to set it ones, no in all controllers, services, etc...:

public function __construct(){
    date_default_timezone_set("Europe/Madrid");
}
like image 809
Argoitz Avatar asked May 11 '18 08:05

Argoitz


2 Answers

If you can’t set it in php.ini, or you want to be sure the value is consistent independently of the target environment, the easiest way is to set it in your AppBundle:

class AppBundle extends Bundle
{
    public function boot()
    {
        parent::boot();
        date_default_timezone_set("Europe/Madrid");
    }
}

Alternatively, if you don’t have an AppBundle (or any other own bundle), you could add it to the AppKernel::registerBundles() method, right before the bundles are loaded.

like image 175
lxg Avatar answered Oct 19 '22 20:10

lxg


<?php

namespace App\EventSubscriber;

use App\Manager\EventManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use Twig\Extension\CoreExtension;

/**
 * Class TimeZoneListener
 *
 * @package App\EventListener
 */
class TimeZoneSubscriber implements EventSubscriberInterface
{
    /** @var Environment */
    private $environment;

    /** @var EventManager */
    private $manager;

    /** @var string */
    private $timezone;

    /**
     * TimeZoneListener constructor.
     *
     * @param Environment  $environment
     * @param EventManager $manager
     * @param string       $timezone
     */
    public function __construct(Environment $environment, EventManager $manager, string $timezone)
    {
        $this->environment = $environment;
        $this->manager     = $manager;
        $this->timezone    = $timezone;
    }

    /**
     * @return array|null
     */
    public static function getSubscribedEvents(): ?array
    {
        return [
            KernelEvents::CONTROLLER => 'setTimeZone',
        ];
    }

    public function setTimeZone(FilterControllerEvent $filterControllerEvent)
    {
        if (!$this->environment instanceof Environment) {
            return;
        }

        // decide correct timezone either default or event defined
        $event    = $this->manager->getActiveEvent();
        $timezone = null === $event ? $this->timezone : $event->getTimezone();

        /**
         * set twig default time zone
         *
         * @var CoreExtension $core
         */
        $core = $this->environment->getExtension('Twig_Extension_Core');
        $core->setTimezone($timezone);

        return $this->environment;
    }
}
like image 30
HelpNeeder Avatar answered Oct 19 '22 18:10

HelpNeeder