Smarty Date_format usage and Chinese garbled solution method

Source: Internet
Author: User
Tags modifier

Using the DATE function in PHP to format the timestamp, you can use Date_format to implement the Smarty
Specific usage:

The code is as follows Copy Code

{$timestamp |date_fomat: "%y-%m-%d%h:%m:%s"}

Note: | No spaces on either side
Output form: 2010-07-10 16:30:25

Other uses are as follows:

The code is as follows Copy Code

{$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"}


instance

date_format[Date format]

index.php:

The code is as follows Copy Code

$smarty = new Smarty;
$smarty->assign (' Yesterday ', Strtotime ('-1 day '));
$smarty->display (' Index.tpl ');

INDEX.TPL:

The code is as follows Copy Code

{$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:

The code is as follows Copy Code

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


Smarty's date_format can't be solved in Chinese.

{$smarty. Now|date_format: "%y year%m Month%d days"} This is the "2010%m Month%d days" + some garbled if the Chinese character is added to the sky after the normal, but the output also has a space.

To solve this problem, read the Smarty plug-in code modifier.date_format.php:
Found inside strftime This PHP function is not good for Chinese support.
So I modified the modifier.date_format.php function, once and for all. You can copy and replace the original content directly.
And I still support Simplified Chinese in this function.

The code is as follows Copy Code
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 (' Year ', ' Month ', ' Day ', ' time ', ' minute ', ' second ', '? 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
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.