How can I set timezone in symfony 3?
Asked Answered
B

3

12

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");
}
Breastpin answered 11/5, 2018 at 8:18 Comment(8)
Why not just set it in php.ini?Litigable
I try it, but it doesnt change in symfony, I don't know why. I try changing the timezone in: /etc/php/7.0/fpm/php.ini, /etc/php/7.0/cli/php.ini and /etc/php/7.0/fpm/php.ini and restarting apache but in symfony the datetime not change.Breastpin
Are you sure you're editing the correct php.ini ? Check out this related question: #20743560Tahsildar
I execute (find / -name "php.ini") and only this files exist in /etc: /etc/php/7.0/apache2/php.ini /etc/php/7.0/cli/php.ini /etc/php/7.0/fpm/php.ini I have other ones in vendor directory of symfony: /var/www/html_back/vendor/doctrine/cache/tests/travis/php.ini /var/www/html_back/vendor/doctrine/doctrine-cache-bundle/Tests/travis/php.ini /var/www/html/vendor/doctrine/cache/tests/travis/php.ini /var/www/html/vendor/doctrine/doctrine-cache-bundle/Tests/travis/php.ini /var/www/html/vendor/vendor/doctrine/cache/tests/travis/php.iniBreastpin
where is your php.ini can you show your route to php.ini?Icebreaker
@ImanaliMamadiev How can i know wat php.ini is used by system? I have this three: /etc/php/7.0/apache2/php.ini /etc/php/7.0/cli/php.ini /etc/php/7.0/fpm/php.inBreastpin
@Argoitzestebanezbarrado The easiest way: from within any controller action just invode phpinfo() and die afterwards. It should dump a whole set of info - and one of them should be location of php.ini used...Punctuality
Thank you @JovanPerovic, I will try it.Breastpin
H
22

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.

Handball answered 5/10, 2018 at 18:56 Comment(1)
Thanks! In Symfony 4 I solved this issue by adding the mentioned line in Kernel.php in the method registerBundles.Pournaras
P
0
<?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;
    }
}
Petrous answered 5/10, 2018 at 14:42 Comment(3)
Do you know how many times you're going to call this function with this solution?Pandurate
Exactly. Whereas if you do it in Boot like the other answer, it's only executed once. Same result, less resource waste.Pandurate
Ah, you're probably right. but i'll keep my answer in case where boot isn't a viable solution.Petrous
A
0

You can check in app/AppKernel.php if timezone for all your symfony app is defined/

It will overpass the default php conf timezone

Addendum answered 29/3, 2021 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.