多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# `date` New in version 1.1: The timezone support has been added in Twig 1.1. New in version 1.5: The default date format support has been added in Twig 1.5. New in version 1.6.1: The default timezone support has been added in Twig 1.6.1. New in version 1.11.0: The introduction of the false value for the timezone was introduced in Twig 1.11.0 The `date` filter formats a date to a given format: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ post.published_at|date("m/d/Y") }} </pre></div></td></tr></table> The format specifier is the same as supported by [date](http://www.php.net/date) [http://www.php.net/date],except when the filtered data is of type [DateInterval](http://www.php.net/DateInterval) [http://www.php.net/DateInterval], when the format must conform to[DateInterval::format](http://www.php.net/DateInterval.format) [http://www.php.net/DateInterval.format] instead. The `date` filter accepts strings (it must be in a format supported by the[strtotime](http://www.php.net/strtotime) [http://www.php.net/strtotime] function), [DateTime](http://www.php.net/DateTime) [http://www.php.net/DateTime] instances, or [DateInterval](http://www.php.net/DateInterval) [http://www.php.net/DateInterval] instances. Forinstance, to display the current date, filter the word "now": <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ "now"|date("m/d/Y") }} </pre></div></td></tr></table> To escape words and characters in the date format use `\\` in front of eachcharacter: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ post.published_at|date("F jS \\a\\t g:ia") }} </pre></div></td></tr></table> If the value passed to the `date` filter is `null`, it will return thecurrent date by default. If an empty string is desired instead of the currentdate, use a ternary operator: ~~~ {{ 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`. Thisdefault can be easily changed by calling the `setDateFormat()` method on the`core` extension instance. The first argument is the default format fordates and the second one is the default format for date intervals: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>$twig = new Twig_Environment($loader); $twig-&gt;getExtension('core')-&gt;setDateFormat('d/m/Y', '%d days'); </pre></div></td></tr></table> ### Timezone By default, the date is displayed by applying the default timezone (the onespecified in php.ini or declared in Twig -- see below), but you can overrideit by explicitly specifying a timezone: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ post.published_at|date("m/d/Y", "Europe/Paris") }} </pre></div></td></tr></table> If the date is already a DateTime object, and if you want to keep its currenttimezone, pass `false` as the timezone value: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ post.published_at|date("m/d/Y", false) }} </pre></div></td></tr></table> The default timezone can also be set globally by calling `setTimezone()`: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>$twig = new Twig_Environment($loader); $twig-&gt;getExtension('core')-&gt;setTimezone('Europe/Paris'); </pre></div></td></tr></table> ### Arguments - `format`: The date format - `timezone`: The date timezone