There are some differences between getting the current date time and formatting the date time in Smarty and PHP. Here we will give you a detailed introduction:
First, obtain the current date and time:
In PHP, we use the date function to obtain the current time. The instance code is as follows:
Date ("Y-m-dH: I: s"); // The result is displayed in the pattern 21:19:36.
However, in the Smarty template, we cannot use date. Instead, we should use now to get the current time. The instance code is as follows:
{$ Smarty. now} // The result is displayed in the timestamp mode of 1280236776.
However, we can also format the timestamp. The instance code is as follows:
{$ Smarty. now | date_format: '% Y-% m-% d % H: % M: % s'} // The result is displayed in the 21:19:36 time mode.
Note that the date_format time Formatting Function in Smarty is basically the same as the strftime () function in PHP. You can view the format identification and conversion mark in the strftime () function in PHP. % Y indicates the year in decimal format, % m indicates the month in decimal format, % d indicates the day in decimal format, % H indicates the hour in decimal format, and % M indicates the score in decimal format, % S indicates the number of seconds in decimal format ).
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////
Usage of the date_format function in smarty
Use the date function in php to format the timestamp, and use date_format in smarty.
Usage:{$ Timestamp | date_fomat: "% Y-% m-% d % H: % M: % S "}Note: | there is no space on either side
Output Format: 16:30:25
Other usage:
{$ 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:
On the template page, use
{$ 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} // format the current time
{$ Smarty. now | date_format: "% H: % M: % S "}
{$ Currtime | date_format} // format the uploaded time.
{$ Currtime | date_format: "% A, % B % e, % Y "}
{$ Currtime | date_format: ":" % Y-% m-% d % H: % M: % S "}
OUTPUT: // OUTPUT the following results
Dec 26,200 8
08:55:25
Dec 26,200 8
Friday, December 26,200 8
2008-08-26 08:55:21