Skip to content

Instantly share code, notes, and snippets.

@jasonbouffard
Created June 16, 2011 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbouffard/1029663 to your computer and use it in GitHub Desktop.
Save jasonbouffard/1029663 to your computer and use it in GitHub Desktop.
Timezone on User Document in UserBundle Twig Extension
....
services:
twig.extension.DateTimeTimeZone:
class: Acme\DemoBundle\Twig\Extension\DateTimeTimeZone
tags:
- { name: twig.extension }
arguments: [ @security.context ]
<?php
// Acme/DemoBundle/Twig/Extension/DateTimeTimeZone.php
namespace Acme\DemoBundle\Twig\Extension;
use Symfony\Component\Security\Core\SecurityContext;
use Acme\DemoBundle\Document\User as User;
class DateTimeTimeZone extends \Twig_Extension
{
private $security;
public function __construct($security = null) {
$this->security = $security;
}
public function getFilters()
{
return array(
'date_time_time_zone' => new \Twig_Filter_Method($this, 'twig_date_time_zone_format_filter'),
);
}
public function getName()
{
return 'DateTimeTimeZone';
}
public function twig_date_time_zone_format_filter(\DateTime $date, $format = 'F j, Y H:i')
{
if ($this->security instanceof SecurityContext) {
$user = $this->security->getToken()->getUser();
if ($user instanceof User) {
$date->setTimezone(new \DateTimeZone($user->getTimeZone()));
}
}
return $date->format($format);
}
}
<div>Created: {{ object.getCreatedAt|date_time_time_zone('F jS \\a\\t g:ia') }}</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment