Smarty Setting the time zone implementation method

Source: Internet
Author: User
Tags current time local time php file set time sprintf

Smarty Time Format (date_format[format date) is more powerful, and its parameters can even be MySQL or Unix timestamp. But there was a slight problem with formatting when you used it:

The code is as follows Copy Code

<{$smarty. Now|date_format: "%y-%m-%d%h:%m"}>

The time it gets is: 2011-06-24 15:58. And my system time is June 24, 2011 23:58, presumably we all understand, this is the time zone set incorrect reason!

The Smarty manual does not find a way to set the time zone, and all of the date_format are in the formatting time, without the option to set the time zone. Baidu did not know that you can set the time zone when instantiating smarty this class. In fact, a careful thought is also very simple, after all, Smarty is written in PHP, and read the template related knowledge people should know: the template is actually the final or include in the source PHP file, but more than a parse, replace the process. So it's like writing a normal PHP file to set the time zone, and finally it will take effect in the template.

The code is as follows Copy Code

Include ROOT. " Smarty/smarty.class.php ";

Date_default_timezone_set (' Asia/shanghai '); Set time zone

$tpl =new Smarty;


------------------------------------------

Want to see what's going on in Smarty's time? Then go ahead ...

Look at the source file generated by the Smarty compilation and find the sentence in the corresponding location:

The code is as follows Copy Code

<?php Echo (Is_array ($_tmp=time ()))?

$this->_run_mod_handler (' Date_format ', true, $_tmp, "%y-%m-%d%h:%m"):

Smarty_modifier_date_format ($_tmp, "%y-%m-%d%h:%m"));

?>

First, the current UNIX timestamp is assigned to the $_TMP variable with time (), and the assignment operation returns a Boolean type of data, but it's a bit confusing to see if it's an array type. Look at the three-mesh operator, when the return is an array of $this->_run_mod_handler (), follow this method to see the plug-in (plugins), seemingly temporarily not use, and do not know when to execute. The Smarty_modifier_date_format () is the key point to see.

The code is as follows Copy Code

function Smarty_modifier_date_format ($string, $format = '%b%e,%Y ', $default _date = ')

{

if ($string!= ') {//If the timestamp string is not empty, format it

$timestamp = Smarty_make_timestamp ($string);

ElseIf ($default _date!= ') {//If the string is empty and the default time is not empty, format the default time

$timestamp = Smarty_make_timestamp ($default _date);

} else {

Return

}

if (directory_separator = = ' \ ') {

$_win_from = Array ('%d ', '%h ', '%n ', '%r ', '%r ', '%t ', '%t ');

$_win_to = Array ('%m/%d/%y ', '%b ', "n", '%i:%m:%s%p ', '%h:%m ', ' t ', '%h:%m:%s ');

if (Strpos ($format, '%e ')!== false) {

$_win_from[] = '%e ';

$_win_to[] = sprintf ('% ' 2d ', date (' J ', $timestamp));

}

if (Strpos ($format, '%l ')!== false) {

$_win_from[] = '%l ';

$_win_to[] = sprintf ('% ' 2d ', date (' H ', $timestamp));

}

$format = Str_replace ($_win_from, $_win_to, $format);

}

Return strftime ($format, $timestamp); This is the final step, and the final timestamp is reformatted according to the current time zone.

}


The former part of this function is primarily intended to be generic, and it is converted to a uniform timestamp based on different string formats or by default time. The Smarty_make_timestamp () function is used:

The code is as follows Copy Code

function Smarty_make_timestamp ($string)

{

Here is to ensure the universality of the time string to transform, so that its format unified. There are comments below, this is not much to say

if (Emptyempty ($string)) {

Use ' Now ':

$time = time ();

} elseif (Preg_match ('/^d{14}$/', $string)) {

It is MySQL timestamp format of YYYYMMDDHHMMSS?

$time = Mktime (substr ($string, 8, 2), substr ($string, 2), substr ($string, 12, 2),

SUBSTR ($string, 4, 2), substr ($string, 6, 2), substr ($string, 0, 4));

} elseif (Is_numeric ($string)) {

It is a numeric string, we handle it as timestamp

$time = (int) $string;

} else {

Strtotime should handle it

$time = Strtotime ($string);

if ($time = = 1 | | $time = = FALSE) {

Strtotime () is not able to parse $string and use "Now":

$time = time ();

}

}

return $time;

}


After the format is sorted, the PHP built-in strftime () function is called, which formats the local time/date according to the locale, and the region is the time zone we set up. The whole process is supposed to be like this.

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.