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

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
smarty use and exist method date hour time format Obtain Detailed explanation

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327711.htmlTechArticleThere are some differences between getting the current date and time and formatting date and time in Smarty and PHP. Here is it for you Detailed introduction: The first is to get the current date and time: In PHP we will...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo Mar 30, 2024 pm 07:26 PM

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 write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

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) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

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! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

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

Where to get Google security code Where to get Google security code Mar 30, 2024 am 11:11 AM

Where to get Google security code

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

How to use Baidu Netdisk app

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

How to use NetEase Mailbox Master

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

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

See all articles