date_format

将日期和时间格式化成strftime()的格式。 时间可以是unix的 时间戳, DateTime 对象, mysql时间戳,或者月日年格式的字符串,与PHP函数strtotime()类似。 模板设计者可以对date_format的格式有完全的控制。 如果传递到date_format的时间为空, 而第二个参数传递了值,那么第二个参数将作为需要格式化的时间。

参数顺序 类型 必选参数 默认值 说明
1 string No %b %e, %Y 输出时间的格式定义
2 string No n/a 如果输入为空,则作为默认时间。

Note

从Smarty-2.6.10开始,给date_format传递 数字值将一直被当作unix时间戳(除了mysql时间戳,看下文)。

在Smarty-2.6.10之前,数字值(如 YYYYMMDD)使用了php函数strtotime()来进行处理, 所以有时候值会被看作时间字符串而不是时间戳(取决于strtotime()的实现)。

唯一的例外mysql时间戳:14位数字值(YYYYMMDDHHMMSS), mysql时间戳比unix时间戳更优先匹配。

程序设计者说明

date_format是派生于PHP函数 strftime()的wrapper。 在PHP编译时strftime()的支持格式的数量将会受到系统影响。 请系统的说明找出完整的格式列表。 然而,部分格式是模拟了windows的行为,如 %D, %e, %h, %l, %n, %r, %R, %t, %T。

Example 5.8. date_format


<?php

$config['date'] = '%I:%M %p';
$config['time'] = '%H:%M:%S';
$smarty->assign('config', $config);
$smarty->assign('yesterday', strtotime('-1 day'));

?>

   

模板使用了 $smarty.now取得当前的时间:


{$smarty.now|date_format}
{$smarty.now|date_format:"%D"}
{$smarty.now|date_format:$config.date}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:$config.time}

   

输出是:


Jan 1, 2022
01/01/22
02:33 pm
Dec 31, 2021
Monday, December 1, 2021
14:33:00

   

date_format支持格式:

参见 $smarty.now, strftime(), {html_select_date}时间技巧文章.