How to obtain the current datetime and format date time using Smarty _php tips

Source: Internet
Author: User
Tags smarty template

Get the current date time and format date time in smarty with some different places in PHP, here is a detailed description:

The first is to get the current date time:
In PHP we use the DATE function to get the current time, and the instance code is as follows:
Date ("Y-m-dh:i:s"); The results will appear as follows: the 2010-07-27 21:19:36 model

But instead of using date in the Smarty template, we should use now to get the current time, with the instance code as follows:
{$smarty. Now}//The result will be displayed as: 1280236776 timestamp mode

However, we can also format the timestamp with the instance code as follows:
{$smarty. Now|date_format: '%y-%m-%d%h:%m:%s '}//The result will be displayed as the time pattern for the 2010-07-27 21:19:36

It should be explained that this Date_format time format function in Smarty is essentially the same as the strftime () function in PHP, and you can see the format recognition conversion tag in the strftime () function in PHP. Where%Y is representing the decimal year,%m is the decimal month,%d is the decimal days,%H is the decimal hour,%m is the decimal point, and%s is the number of seconds on behalf of the decimal (the S is capitalized here).
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////
date_format function usage in Smarty
Using the DATE function in PHP to format the timestamp, you can use Date_format to implement the Smarty
Specific usage:{$timestamp |date_fomat: "%y-%m-%d%h:%m:%s"} Note: | There is no space on either side
Output form: 2010-07-10 16:30:25
Other uses 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

In 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 passed in time
{$currtime |date_format: "%A,%B%e,%Y"}
{$currtime |date_format: ":%y-%m-%d%h:%m:%s"}

output://above output the following results

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.