Home >Backend Development >PHP Tutorial >Detailed explanation of how to use Smarty to obtain the current date and time and format the date and time_PHP Tutorial
Getting the current date and time and formatting the date and time in Smarty are somewhat different from those in PHP. Here is a detailed introduction for you:
First, get the current date and time:
In PHP we will use the date function to get the current time. The example code is as follows:
date(" Y-m-dH:i:s"); //The result will be displayed as: 2010-07-27 21:19:36 mode
But we cannot use date in the Smarty template. Instead, we should use now to get the current time. The example code is as follows:
{$smarty.now} //The result will be displayed as: 1280236776 time Poke mode
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 pattern of 2010-07-27 21:19:36
It should be noted that the date_format time formatting function in Smarty is basically the same as the strftime() function in PHP. You can check the format identification conversion mark in the strftime() function in PHP. Among them, %Y represents the decimal year, %m represents the decimal month, %d represents the decimal day, %H represents the decimal hour, %M represents the decimal fraction, and %S represents the decimal second (the S here It’s in capital letters).
////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// //
Usage of date_format function in smarty
Use date function in php to format timestamp. Date_format can be used in smarty to achieve
Specific usage: {$timestamp|date_fomat:”%Y-%m-%d %H:%M:%S”} Note: | There are no spaces on both sides
Output format: 2010-07- 10 16:30:25
Other usages are as follows:
{$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:
Use
{$goods.add_time|date_format:"%Y-%m-%d %H:%M:%S"}
---------- on the template page ----------------
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 passed time
{$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