2016-11-02 17 views
5

Mam ciąg reprezentujący numer tygodnia i rok i chciałbym wypisać w niedzielę tego tygodnia.Czy istnieje alternatywa gałązka dla DateTime :: createFromFormat (...)?

Przykład w PHP:

$dateString = '502016'; 
$dateObject = DateTime::createFromFormat('WY', $dateString); 
echo $dateObject->format('Y-m-d'); 

Coś jak:

{{ published_at|format(date("Ymd"), 'd-m-Y') }} 

Jest to możliwe w gałązki?

+0

próbowałeś użyć filtru/funkcji 'date'? –

+0

@RuslanOsmanov Skąd wiadomo, że '112016' ma format na tydzień? Nie mogę podać w formacie źródłowym. – Peter

+1

. Wygląda na to, że nie możesz użyć słowa "date" tutaj. Ale myślę, że powinieneś raczej przekonwertować '$ dateString' na format odpowiedni dla' date'. W przeciwnym razie musisz napisać własny filtr/funkcję. –

Odpowiedz

2

Znalazłem coś, co może pomóc: overriding the built in twig date filter

To tworząc nowe rozszerzenie gałązka o co chcesz :)

PS: Na czym kończy zwróci PHP DateTime

<?php 
/* 
* Extension to provide updated date & date_modify filters along with an 
* updated date function which do not auto-convert strings of numbers to 
* a unix timestamp. 
* 
* Code within dateFilter(), modifyFilter() and dateFromString() extracted 
* from Twig/lib/Twig/Extension/Core.php which is (c) 2009 Fabien Potencier 
* and licensed as per https://github.com/twigphp/Twig/blob/master/LICENSE 
*/ 
namespace My\Twig\Extension; 

use Twig_Extension; 
use Twig_SimpleFilter; 
use Twig_SimpleFunction; 
use DateTime; 
use DateTimeInterface; 
use DateTimeImmutable; 

class DateExtension extends Twig_Extension 
{ 
    public function getName() 
    { 
     return 'my_date'; 
    } 

    public function getFilters() 
    { 
     return array(
      new Twig_SimpleFilter('date', [$this, 'dateFilter'], 
        ['needs_environment' => true]), 
      new Twig_SimpleFilter('date_modify', [$this, 'modifyFilter'], 
        ['needs_environment' => true]), 
     ); 
    } 

    public function getFunctions() 
    { 
     return array(
      new Twig_SimpleFunction('date', [$this, 'dateFromString'], 
        ['needs_environment' => true]), 
     ); 
    } 

    public function dateFilter($env, $date, $format = null, $timezone = null) 
    { 
     if (null === $format) { 
      $formats = $env->getExtension('core')->getDateFormat(); 
      $format = $date instanceof DateInterval ? $formats[1] : $formats[0]; 
     } 

     if ($date instanceof DateInterval) { 
      return $date->format($format); 
     } 

     return $this->dateFromString($env, $date, $timezone)->format($format); 
    } 

    public function modifyFilter($env, $date, $format = null, $timezone = null) 
    { 
     $date = $this->dateFromString($env, $date, false); 
     $date->modify($modifier); 

     return $date; 
    } 

    public function dateFromString($env, $date, $timezone) 
    { 
     // determine the timezone 
     if (!$timezone) { 
      $defaultTimezone = $env->getExtension('core')->getTimezone(); 
     } elseif (!$timezone instanceof DateTimeZone) { 
      $defaultTimezone = new DateTimeZone($timezone); 
     } else { 
      $defaultTimezone = $timezone; 
     } 

     // immutable dates 
     if ($date instanceof DateTimeImmutable) { 
      return false !== $timezone ? $date->setTimezone($defaultTimezone) : $date; 
     } 

     if ($date instanceof DateTime || $date instanceof DateTimeInterface) { 
      $date = clone $date; 
      if (false !== $timezone) { 
       $date->setTimezone($defaultTimezone); 
      } 

      return $date; 
     } 

     $date = new DateTime($date, $defaultTimezone); 
     if (false !== $timezone) { 
      $date->setTimezone($defaultTimezone); 
     } 

     return $date; 
    } 
} 

Ta klasa po prostu rejestruje nową datę, filtry date_modify i nową funkcję daty, aby zastąpić te w rdzeniu Twig, a następnie jest bezpośrednią kopią funkcji twig_date_format_filter, twig_date, w postaci _modify_filter i twig_date_converter z powyższą funkcją.

Musiałem także zarejestrować to rozszerzenie z Twig_Environment używając:

$env->addExtension(new \My\Twig\Extension\DateExtension());

i skończę.

{{ "20141216"|date('jS F Y') }} 
{{ "20141216"|date('jS F Y') }} 

teraz prawidłowo Wyjścia: 16 grudnia 2014 16 grudnia 2014

Mimo, że to wstyd, nie mogę po prostu przesłanianie twig_date_converter, cieszę się, że mogę ponownej rejestracji odpowiednie filtry i funkcja Twig.

+0

Myślałem o stworzeniu własnego rozszerzenia, ale zastanawiałem się, czy istnieje istniejące rozwiązanie. – Peter

+1

Po prostu nie publikuj linków jako odpowiedzi, ponieważ adresy URL mogą z czasem znikać – DarkBee

Powiązane problemy