Obtaining the current date and time in Smarty and formatting the date and time are somewhat different from those in PHP. Here is a detailed introduction for you:
The first is to obtain the current date and time:
In PHP, we will use the date function to obtain the current time. Example The code is as follows:
date("Ym-dH:i:s"); //The result will be displayed as: 2010-07-27 21:19:36 pattern
But in Smarty template we can't use date, but You should use now to get the current time, the example code is as follows:
{$smarty.now} //The result will be displayed as: timestamp mode of 1280236776
However, we can also format this timestamp, the example code is as follows:
{$smarty .now|date_format:'%Y-%m-%d %H:%M:%S'} //The result will be displayed as the time mode of 2010-07-27 21:19:36 It
should be noted that in Smarty The date_format time formatting function is basically the same as the strftime() function in PHP, you can go to the format recognition conversion flag in the strftime() function in PHP. Where %Y is the year in decimal, %m is the month in decimal, %d is the day in decimal, %H is the hour in decimal, %M is the fraction in decimal, and %S is the second in decimal (here S is capitalized).
The first is to obtain the current date and time:
In PHP, we will use the date function to obtain the current time. Example The code is as follows:
date("Ym-dH:i:s"); //The result will be displayed as: 2010-07-27 21:19:36 pattern
But in Smarty template we can't use date, but You should use now to get the current time, the example code is as follows:
{$smarty.now} //The result will be displayed as: timestamp mode of 1280236776
However, we can also format this timestamp, the example code is as follows:
{$smarty .now|date_format:'%Y-%m-%d %H:%M:%S'} //The result will be displayed as the time mode of 2010-07-27 21:19:36 It
should be noted that in Smarty The date_format time formatting function is basically the same as the strftime() function in PHP, you can go to the format recognition conversion flag in the strftime() function in PHP. Where %Y is the year in decimal, %m is the month in decimal, %d is the day in decimal, %H is the hour in decimal, %M is the fraction in decimal, and %S is the second in decimal (here S is capitalized).
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Usage of date_format function in smarty |
The date function is used in php to format the timestamp, and date_format can be used in smarty to achieve
specific usage: {$timestamp|date_fomat:”%Y-%m-%d %H:%M:%S”} Note: | No spaces on both sides
Output form: 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”}
eg: