Use Smarty to get the current date and time and format the date and time. Getting the current date and time and formatting the date in Smarty are somewhat different from those in PHP. here we will introduce you in detail: first, getting the current date and time: in PHP, we will get the current date and time and format the date in Smarty, which are different from those in PHP. here we will introduce you in detail:
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
Getting the current date and time and formatting the date in the format are somewhat different from those in PHP. here we will introduce you in detail: first, getting the current date and time: in PHP, we will...