How to Use Smarty to get the current date and time and format the date and time

Source: Internet
Author: User
This article provides a detailed analysis of how to use Smarty to obtain the current date and time and format the date and time. For more information, see

This article provides a detailed analysis of how to use Smarty to obtain the current date and time and format the date and time. For more information, see

There are some differences between getting the current date time and formatting date time in Smarty and PHP. The VM will be detailed here:

First, obtain the current date and time:
In PHP, we use the date function to obtain the current time. The instance code is as follows:
Date ("Y-m-dH: I: s"); // The result is displayed in the pattern 21:19:36.

However, in the Smarty template, we cannot use date. Instead, we should use now to get the current time. The instance code is as follows:
{$ Smarty. now} // The result is displayed in the timestamp mode of 1280236776.

However, we can also format the timestamp. The instance code is as follows:
{$ Smarty. now | date_format: '% Y-% m-% d % H: % M: % s'} // The result is displayed in the 21:19:36 time mode.

Note that the date_format time Formatting Function in Smarty is basically the same as the strftime () function in PHP. You can view the format identification and conversion mark in the strftime () function in PHP. % Y indicates the year in decimal format, % m indicates the month in decimal format, % d indicates the day in decimal format, % H indicates the hour in decimal format, Hong Kong server, and % M indicates the score in decimal format, american space. % S indicates the number of seconds in decimal format (Here S is in uppercase ).
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////
Usage of the date_format function in smarty
Use the date function in php to format the timestamp, and use date_format in smarty.
Usage:{$ Timestamp | date_fomat: "% Y-% m-% d % H: % M: % S "}Note: | there is no space on either side
Output Format: 16:30:25
Other usage:
{$ 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:

On 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 the uploaded time.
{$ Currtime | date_format: "% A, % B % e, % Y "}
{$ Currtime | date_format: ":" % Y-% m-% d % H: % M: % S "}

OUTPUT: // OUTPUT the following results

Dec 26,200 8
08:55:25
Dec 26,200 8
Friday, December 26,200 8
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.