Created
June 16, 2011 16:46
-
-
Save jasonbouffard/1029663 to your computer and use it in GitHub Desktop.
Timezone on User Document in UserBundle Twig Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
services: | |
twig.extension.DateTimeTimeZone: | |
class: Acme\DemoBundle\Twig\Extension\DateTimeTimeZone | |
tags: | |
- { name: twig.extension } | |
arguments: [ @security.context ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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