Smarty get current datetime and formatted datetime

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).

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

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:

 

Used in template pages
{$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 time passed
{$currtime|date_format:"%A, %B %e, %Y"}
{$currtime|date_format:":"%Y-%m-%d %H:%M:%S"}

OUTPUT://The above outputs the following results

Dec 26, 2008
08:55:25
Dec 26, 2008
Friday, December 26, 2008
2008-08-26 08:55:21

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325004103&siteId=291194637