date_format

Parameter PositionTypeRequiredDefaultDescription
1stringNo%b %e, %YThis is the format for the outputted date.
2stringNon/aThis is the default date if the input is empty.

This formats a date and time into the given strftime() format. Dates can be passed to Smarty as unix timestamps, mysql timestamps or any string made up of month day year (parsable by strtotime). Designers can then use date_format to have complete control of the formatting of the date. If the date passed to date_format is empty and a second parameter is passed, that will be used as the date to format.

此函式使用 strftime() 函式將日期與時間格式化。在傳到 Smarty 前,時間格式可 以是 unix 或 mysql 的時間戳記格式,或者是自定的格式(使用 strtotime 函式分析)。設計 者可以使用 date_format 函式控制日期與時間的格式,如果傳給 date_format 函式時日期是空 白的而只有秒的參數的話,秒的參數會被視為日期的參數而被格式化。

Example 5-8. date_format

<?php
    
$smarty
= new Smarty;
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
    
?>

Where index.tpl is:

其中 index.tpl 的內容為:

{$smarty.now|date_format}
{$smarty.now|date_format:"%A, %B %e, %Y"}
{$smarty.now|date_format:"%H:%M:%S"}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:"%H:%M:%S"}

This will output:

輸出結果為:

Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00

date_format conversion specifiers:

date_format 轉換參數:

Programmers note: date_format is essentially a wrapper to PHP's strftime() function. You may have more or less conversion specifiers available depending on your system's strftime() function where PHP was compiled. Check your system's manpage for a full list of valid specifiers.

程式人員提醒: date_format 基本上是 PHP strftime() 函式的包裝函式,每個人會因為自己的系統在編 譯 PHP 時狀況的不同而有不同的參數,因此請聯絡你的系統的管理者以了解你可以使用的參數。