


Detailed explanation of how to use Smarty to obtain the current date and time and format the date and time_PHP Tutorial
Jul 21, 2016 pm 03:05 PM
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

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)
