注意:| 两边没有空格
输出形式: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”}
实例
date_format[日期格式]
index.php:
$smarty = new Smarty;
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
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"}
OUTPUT:
Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00
smarty的date_format中不能有中文的解决
{$smarty.now|date_format:"%Y年%m月%d日"} 这样写就成了"2010年%m月%d日"+一些乱码如果给汉字后加上空格就正常了,但是输出也有了空格。
为了解决这个问题,读了smarty的插件代码modifier.date_format.php:
发现里面strftime这个PHP函数对于中文支持不好。
所以我修改了modifier.date_format.php函数,一劳永逸啊。大家可以直接复制替换原有内容即可。
而且我这个函数还是支持繁简中文的
function smarty_modifier_date_format($string, $format = '%b %e, %Y', $default_date = '')
{
if (substr(PHP_OS,0,3) == 'WIN') {
$_win_from = array ('%e', '%T', '%D');
$_win_to = array ('%#d', '%H:%M:%S', '%m/%d/%y');
$format = str_replace($_win_from, $_win_to, $format);
}
$arrTemp = array('年','月','日','时','分','秒','?r');
foreach($arrTemp as $v){
if(strpos($format,$v)){
$strFormat = str_replace('%','',$format);
}
}
if($string != '') {
if(!emptyempty($strFormat)) return date($strFormat, smarty_make_timestamp($string));
else return strftime($format, smarty_make_timestamp($string));
} elseif (isset($default_date) && $default_date != '') {
if(!emptyempty($strFormat)) return date($strFormat, smarty_make_timestamp($default_date));
else return strftime($format, smarty_make_timestamp($default_date));
} else {
return;
}
}