date

The date filter formats a date to a given format:

日付フィルターは、日付を特定の形式にフォーマットします。
1
{{ post.published_at|date("m/d/Y") }}

The format specifier is the same as supported by date, except when the filtered data is of type DateInterval, when the format must conform to DateInterval::format instead.

フォーマット指定子は、フィルター処理されたデータが DateInterval 型である場合を除いて、date でサポートされているものと同じです。フォーマットは代わりに DateInterval::format に準拠する必要があります。

The date filter accepts strings (it must be in a format supported by the strtotime function), DateTime instances, or DateInterval instances. For instance, to display the current date, filter the word "now":

日付フィルターは、文字列 (strtotime 関数でサポートされている形式である必要があります)、DateTime インスタンス、または DateInterval インスタンスを受け入れます。たとえば、現在の日付を表示するには、「今」という単語をフィルター処理します。
1
{{ "now"|date("m/d/Y") }}

To escape words and characters in the date format use \\ in front of each character:

日付形式の単語や文字をエスケープするには、各文字の前に \\ を使用します。
1
{{ post.published_at|date("F jS \\a\\t g:ia") }}

If the value passed to the date filter is null, it will return the current date by default. If an empty string is desired instead of the current date, use a ternary operator:

日付フィルターに渡された値が null の場合、デフォルトで現在の日付が返されます。現在の日付の代わりに空の文字列が必要な場合は、三項演算子を使用します。
1
{{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}

If no format is provided, Twig will use the default one: F j, Y H:i. This default can be changed by calling the setDateFormat() method on the core extension instance. The first argument is the default format for dates and the second one is the default format for date intervals:

フォーマットが指定されていない場合、Twig はデフォルトの F j, Y H:i を使用します。このデフォルトは、コア拡張インスタンスで setDateFormat() メソッドを呼び出すことによって変更できます。最初の引数は日付の既定の形式で、2 番目の引数は日付間隔の既定の形式です。
1
2
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setDateFormat('d/m/Y', '%d days');

Timezone

By default, the date is displayed by applying the default timezone (the one specified in php.ini or declared in Twig -- see below), but you can override it by explicitly specifying a timezone:

デフォルトでは、日付はデフォルトのタイムゾーン (php.ini で指定されたもの、または Twig で宣言されたもの -- 以下を参照) を適用することによって表示されますが、タイムゾーンを明示的に指定することでオーバーライドできます。
1
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}

If the date is already a DateTime object, and if you want to keep its current timezone, pass false as the timezone value:

日付がすでに DateTime オブジェクトであり、現在のタイムゾーンを維持したい場合は、タイムゾーンの値として false を渡します。
1
{{ post.published_at|date("m/d/Y", false) }}

The default timezone can also be set globally by calling setTimezone():

setTimezone() を呼び出して、デフォルトのタイムゾーンをグローバルに設定することもできます。
1
2
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris');

Arguments

  • format: The date format
    format: 日付形式
  • timezone: The date timezone
    timezone: 日付のタイムゾーン