How to get current datetime and format date time using Smarty _php tutorial

Source: Internet
Author: User
Tags smarty template
Getting the current datetime and formatted date time in Smarty is a bit different from PHP, here's a detailed description:

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

But in the Smarty template we cannot use date, but we should use now to get the current time, the instance code is as follows:
{$smarty. Now}//The result will be shown as: 1280236776 timestamp mode

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

It is necessary to note that this Date_format time format function in Smarty is basically the same as the strftime () function in PHP, and you can see the format Recognition transformation token in the strftime () function in PHP. Where%Y is the decimal year,%m is the decimal month,%d is the decimal number of days,%H is the decimal hour,%m is the decimal fraction, and%s is the number of seconds in decimal (where S is capitalized).
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////
date_format function usage in Smarty
Using the DATE function in PHP to format timestamps, you can use Date_format in Smarty to implement
Specific usage:{$timestamp |date_fomat: "%y-%m-%d%h:%m:%s"}Note: | No spaces 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

On the template page, use the
{$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 Current time
{$smarty. Now|date_format: "%h:%m:%s"}
{$currtime |date_format}//the time the format was passed
{$currtime |date_format: "%A,%B%e,%Y"}
{$currtime |date_format: ":"%y-%m-%d%h:%m:%s "}

output://above outputs the following results

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

http://www.bkjia.com/PHPjc/327711.html www.bkjia.com true http://www.bkjia.com/PHPjc/327711.html techarticle getting the current datetime and formatted date time in Smarty is a bit different from PHP, here's a detailed description: First, get the current datetime: in PHP we will ...

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