PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Smarty教程 > 正文

    smarty出现中文乱码解决方法和date_format用法

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:在php中使用date函数来格式化时间戳,smarty中可以使用date_format来实现具体用法:{$timestamp|date_fomat:%Y-%m-%d %H:%M:%S}注意:| ...
    在php中使用date函数来格式化时间戳,smarty中可以使用date_format来实现
    具体用法:
    {$timestamp|date_fomat:”%Y-%m-%d %H:%M:%S”}

    注意:| 两边没有空格
    输出形式: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;    
        }    
       
    }   

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-84-348-1.html
    相关热词搜索: date_format 中文乱码