|
在Smarty 中獲取當(dāng)前日期時(shí)間和格式化日期時(shí)間與php中有些不同的地方,這里就為您詳細(xì)介紹:
首先是獲取當(dāng)前的日期時(shí)間:
在php中我們會(huì)使用date函數(shù)來(lái)獲取當(dāng)前的時(shí)間,實(shí)例代碼如下:
date("Y-m-dH:i:s"); //該結(jié)果會(huì)顯示為:2010-07-27 21:19:36 的模式
但是在Smarty 模板中我們就不能使用date 了,而是應(yīng)該使用 now 來(lái)獲取當(dāng)前的時(shí)間,實(shí)例代碼如下:
{$smarty.now} //該結(jié)果會(huì)顯示為:1280236776的時(shí)間戳模式
然而我們還可以將這個(gè)時(shí)間戳格式化,實(shí)例代碼如下:
{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} //該結(jié)果會(huì)顯示為 2010-07-27 21:19:36 的時(shí)間模式
需要說(shuō)明的是 Smarty 中的這個(gè)date_format 時(shí)間格式化函數(shù)和php中的 strftime()函數(shù)基本上相同,您可以去查看php中的 strftime() 函數(shù)中的format 識(shí)別轉(zhuǎn)換標(biāo)記。其中 %Y 是代表十進(jìn)制年份,%m是代表十進(jìn)制月份,%d 是代表十進(jìn)制天數(shù),%H 是代表十進(jìn)制小時(shí)數(shù),%M是代表十進(jìn)制的分?jǐn)?shù),%S是代表十進(jìn)制的秒數(shù)(這里的S是大寫(xiě)的哦)。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
smarty中date_format函數(shù)用法
在php中使用date函數(shù)來(lái)格式化時(shí)間戳,smarty中可以使用date_format來(lái)實(shí)現(xiàn)
具體用法:{$timestamp|date_fomat:”%Y-%m-%d %H:%M:%S”} 注意:| 兩邊沒(méi)有空格
輸出形式:2010-07-10 16:30:25
其他用法如下:
{$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”}
eg:
在模板頁(yè)用
{$goods.add_time|date_format:"%Y-%m-%d %H:%M:%S"}
--------------------------
index.php:
$smarty = new Smarty;
$smarty->assign('currtime', time());
$smarty->display('index.tpl');
index.tpl:
{$smarty.now|date_format}//格式化當(dāng)前時(shí)間
{$smarty.now|date_format:"%H:%M:%S"}
{$currtime|date_format}//格式化傳過(guò)來(lái)的時(shí)間
{$currtime|date_format:"%A, %B %e, %Y"}
{$currtime|date_format:":"%Y-%m-%d %H:%M:%S"}
OUTPUT://以上輸出以下結(jié)果
Dec 26, 2008
08:55:25
Dec 26, 2008
Friday, December 26, 2008
2008-08-26 08:55:21
php技術(shù):使用Smarty 獲取當(dāng)前日期時(shí)間和格式化日期時(shí)間的方法詳解,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。