suchen
Heimphp教程php手册使用Smarty 获取当前日期时间和格式化日期时间的方法详解

本篇文章是对使用Smarty获取当前日期时间和格式化日期时间的方法进行了详细的分析介绍,需要的朋友参考下

在Smarty 中获取当前日期时间和格式化日期时间与PHP中有些不同的地方,虚拟主机,这里就为您详细介绍:

首先是获取当前的日期时间:
在PHP中我们会使用date函数来获取当前的时间,实例代码如下:
date("Y-m-dH:i:s"); //该结果会显示为:2010-07-27 21:19:36 的模式

但是在Smarty 模板中我们就不能使用date 了,而是应该使用 now 来获取当前的时间,实例代码如下:
{$smarty.now} //该结果会显示为:1280236776的时间戳模式

然而我们还可以将这个时间戳格式化,实例代码如下:
{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} //该结果会显示为 2010-07-27 21:19:36 的时间模式

需要说明的是 Smarty 中的这个date_format 时间格式化函数和PHP中的 strftime()函数基本上相同,您可以去查看PHP中的 strftime() 函数中的format 识别转换标记。其中 %Y 是代表十进制年份,%m是代表十进制月份,%d 是代表十进制天数,%H 是代表十进制小时数,香港服务器,%M是代表十进制的分数,美国空间,%S是代表十进制的秒数(这里的S是大写的哦)。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
smarty中date_format函数用法
在php中使用date函数来格式化时间戳,smarty中可以使用date_format来实现
具体用法:{$timestamp|date_fomat:”%Y-%m-%d %H:%M:%S”} 注意:| 两边没有空格
输出形式:2010-07-10 16:30:25
其他用法如下:
{$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:

在模板页用
{$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}//格式化当前时间
{$smarty.now|date_format:"%H:%M:%S"}
{$currtime|date_format}//格式化传过来的时间
{$currtime|date_format:"%A, %B %e, %Y"}
{$currtime|date_format:":"%Y-%m-%d %H:%M:%S"}

OUTPUT://以上输出以下结果

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

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat -Befehle und wie man sie benutzt
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Dreamweaver Mac

Dreamweaver Mac

Visuelle Webentwicklungstools

EditPlus chinesische Crack-Version

EditPlus chinesische Crack-Version

Geringe Größe, Syntaxhervorhebung, unterstützt keine Code-Eingabeaufforderungsfunktion

Herunterladen der Mac-Version des Atom-Editors

Herunterladen der Mac-Version des Atom-Editors

Der beliebteste Open-Source-Editor

VSCode Windows 64-Bit-Download

VSCode Windows 64-Bit-Download

Ein kostenloser und leistungsstarker IDE-Editor von Microsoft

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)