date - Symfony convert to user's timezone in controller or twig -
i store in database dates of time datetime on mysql on utc time. i'm using symfony 2.4.5 (yes old).
i retrieve dates controller normal sql query propel. since not want query user timezone time database save in session user's timezone '{'timezone'=> 'europe/paris'}.
should convert dates retrieved db in controller or in twig ?
// in controller $dictionarythathaveotherdata = ... $datefromdbinutc = userquery::create()->... $datefromdbinutc->settimezone(new datetimezone($this->get('session')->get('timezone'))); $dictionarythathaveotherdata['dateforsomething'] = $datefromdbinutc; // in twig {{ dateforsomething | date('theformatiwant') }}
or instead convert in twig:
// in controller $dictionarythathaveotherdata = ... $datefromdbinutc = userquery::create()->... $dictionarythathaveotherdata['dateforsomething'] = $datefromdbinutc; // in twig {{ dateforsomething | date('theformatiwant', app.session.timezone) }}
i guess better in controller view shall care showing data in desired format , not involves many things thinking if date in timezone or not.
what if view needs comparisons between dates, better in utc or user timezone ? make difference ?
edit since answer darkbee:
first tried in twig view:
- date('h:i:s', user_timezone.zone)
- date('h:i:s', app.session.get('timezone'))
- date('h:i:s')
- date('h:i:s', 'europe/paris')
- date('h:i:s', 'asia/shanghai')
date('h:i:s', false)
send view utc timezone
19:00:00
- 19:00:00
- 11:00:00
- 13:00:00
- 19:00:00
11:00:00
send view shanghai timezone
19:00:00
- 19:00:00
- 11:00:00
- 13:00:00
- 19:00:00
19:00:00
in controller :
$this->get('twig')->getextension('core')->settimezone('europe/paris');
19:00:00
- 19:00:00
- 13:00:00
- 13:00:00
- 19:00:00
11:00:00
in controller:
$this->get('twig')->getextension('core')->settimezone($this->get('session')->get('timezone'));
19:00:00
- 19:00:00
- 19:00:00
- 13:00:00
- 19:00:00
11:00:00
in controller , date shanghai timezone :
$this->get('twig')->getextension('core')->settimezone('europe/paris');
19:00:00
- 19:00:00
- 13:00:00
- 13:00:00
- 19:00:00
- 19:00:00
so yes see 'date('h:i:s')' time when set twig timezone
thank you, guess managed make work i'm not sure if , not have problems multi-users (i not know how symfony works multi-thread or multi-request different users) @ same time do:
$this->twig->getextension('core')->settimezone($user->gettimezone()->getzone());
i tried 2 browsers fetching different pages , setting new timezone each different user logged in. works , not override other user's timezone. thought because changes default timezone twig core removed line modify timezone, after user logged in , has set value, reloaded page , set utc.
does mean global variables 1 request ?
i not used symfony2 handling requests , how works variables , cache.
i looked many solutions: example example2 example3
my code:
class twigdaterequestlistener { protected $twig; private $context; function __construct(\twig_environment $twig, securitycontext $context) { $this->twig = $twig; $this->context = $context; } public function onkernelrequest(getresponseevent $event) { $user = $this->context->gettoken()->getuser(); if ($user != 'anon.') { $user = saasuserquery::create()->findpk($user->getid()); $this->twig->getextension('core')->settimezone($user->gettimezone()->getzone()); } else { $this->twig->getextension('core')->settimezone('europe/paris'); } } }
then in view need write 'date('h:i:s')', , user's timezone , if not logged in on paris timezone.
is good? not sure multi-request different users @ same time. example:
important: 1 user make request in controller action method (the timezone has been set), second user make request has different timezone first user. both controller make call render , view rendered , returned users.
precisely here, happens ? both dates in respective timezone or first user dates in second user timezone ?
i go third method can reduce lot of code. can set default timezone date filter in twig
$twig = new twig_environment($loader); $twig->getextension('core')->settimezone('europe/paris');
Comments
Post a Comment