11

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");
}
Argoitz
  • 339
  • 1
  • 3
  • 15
  • 1
    Why not just set it in php.ini? – Iwan Wijaya May 11 '18 at 08:32
  • 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. – Argoitz May 11 '18 at 08:39
  • 1
    Are you sure you're editing the correct php.ini ? Check out this related question: https://stackoverflow.com/questions/20743060/symfony2-and-date-default-timezone-get-it-is-not-safe-to-rely-on-the-system – fxbt May 11 '18 at 08:42
  • 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.ini – Argoitz May 11 '18 at 08:49
  • where is your `php.ini` can you show your route to `php.ini`? – Imanali Mamadiev May 11 '18 at 09:01
  • @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.in – Argoitz May 11 '18 at 09:49
  • @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... – Jovan Perovic May 11 '18 at 13:39
  • Thank you @JovanPerovic, I will try it. – Argoitz May 14 '18 at 10:36

3 Answers3

20

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.

lxg
  • 12,375
  • 12
  • 51
  • 73
  • 2
    Thanks! In Symfony 4 I solved this issue by adding the mentioned line in `Kernel.php` in the method `registerBundles`. – cezar Aug 07 '19 at 07:56
1
<?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;
    }
}
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • Do you know how many times you're going to call this function with this solution? – Jimbo Jun 25 '20 at 18:10
  • 1
    Exactly. Whereas if you do it in Boot like the other answer, it's only executed once. Same result, less resource waste. – Jimbo Jun 26 '20 at 18:43
  • Ah, you're probably right. but i'll keep my answer in case where boot isn't a viable solution. – HelpNeeder Jun 26 '20 at 19:07
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

Julien
  • 41
  • 3