php中格式化输出日期和时间可用:date('Y-m-d H:i:s',时间戳);
的形式输出,对应的是“年-月-日 时:分:秒”。
而在smarty模板中,如$time是php文件中assign过来的时间戳,在模板文件中写法为:
<{$time|date_format:'%Y-%m-%d %H:%M:%S'}>
,同样对应的输出格式为:“年-月-日 时:分:秒”。
php文件:
<?php
//导入自定义smarty操作类SmartyInit.php
include_once('class/SmartyInit.php');
$smarty = new SmartyInit();
//设置默认时区为上海
date_default_timezone_set('Asia/Shanghai');
//输出echo strtotime('now'),结果如:1245763672
//可知strtotime('now')返回的是时间戳
//也可是从数据库得到的时间戳
$time = time();
echo 'php格式化输出:<br />';
echo '昨天:'.date('Y-m-d H:i:s', strtotime('-1 day')).'<br />';
//date('Y-m-d H:i:s'),不写第二个参数,默认为当前时间
//也可写为:date('Y-m-d H:i:s', strtotime('now'))
echo '今天:'.date('Y-m-d H:i:s').'<br />';
echo '明天:'.date('Y-m-d H:i:s', strtotime('1 day')).'<br />';
echo '赋值时间戳:'.date('Y-m-d H:i:s', $time).'<br />';
//strtotime('today')只输出当天日期,
//strtotime('today 00:00:00')可输出时间
$smarty->assign('yesterday', strtotime('yesterday'));
$smarty->assign('today', strtotime('today 20:15:04'));
$smarty->assign('tomorrow', strtotime('tomorrow'));
$smarty->assign('yesterday1', strtotime('-1 day'));
//等同$smarty->assign('today1', strtotime('0 day'));
$smarty->assign('today1', strtotime('now'));
$smarty->assign('tomorrow1', strtotime('1 day'));
$smarty->assign('time', $time);
$smarty->display('index.html');
模板文件(html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>smarty测试</title>
</head>
<body>
<p>smarty模板输出:<br />
昨天:<{$yesterday|date_format:'%Y-%m-%d %H:%M:%S'}>
<br />
今天:<{$today|date_format:'%Y-%m-%d %H:%M:%S'}>
<br />
明天:<{$tomorrow|date_format:'%Y-%m-%d %H:%M:%S'}>
</p>
<p>
昨天:<{$yesterday1|date_format:'%Y-%m-%d %H:%M:%S'}>
<br />
今天:<{$today1|date_format:'%Y-%m-%d %H:%M:%S'}>
<br />
明天:<{$tomorrow1|date_format:'%Y-%m-%d %H:%M:%S'}>
<br />
赋值时间戳:<{$time|date_format:'%Y-%m-%d %H:%M:%S'}>
</p>
smarty保留变量输出:<{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}>
</body>
</html>
运行结果:
- index.rar (700 Bytes)
- 描述: php文件index.php
- 下载次数: 6
- index.rar (500 Bytes)
- 描述: 模板文件index.html
- 下载次数: 4
分享到:
相关推荐
php+smarty输出资讯列表,生成html静态
php Smarty date_format [格式化时间日期] ,需要的朋友可以参考下。
Smarty格式化数字 default默认 escape转码 indent缩进 lower小写 nl2br换行符替换成 regex_replace正则替换 replace替换 spacify spacify是在字符串的每个字符之间插入空格或者其他的字符串. 参数表示将在两个...
php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)
本篇文章是对使用Smarty获取当前日期时间和格式化日期时间的方法进行了详细的分析介绍,需要的朋友参考下
资源名称:PHP Smarty中文手册 chm格式内容简介: 早在1999年后期,我们就已经开始为模板引擎写说明文档。在完成这个文档之后,我们开始用c写一个模板引擎,并有希望被包含到php里去。在 撞上了许多的技术难题的同时...
php之smarty入门
php和smarty中文chm手册,包含smarty源码
PHP Smarty通过gettext制作多语言版本
Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。简单的讲,目的就是...
smarty中section的使用 smarty section php
date_format [格式化日期] default [默认值] escape [编码] indent [缩进] lower [小写] nl2br [换行符替换成 ] regex_replace [正则替换] replace [替换] spacify [插空] string_format [字符串格式...
smarty中文手册smarty中文手册smarty中文手册smarty中文手册smarty中文手册smarty中文手册smarty中文手
SMARTY中文模板手册
Smarty分离了逻辑代码和外在的内容,提供一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。Smarty工作的目的是要使PHP程序员同前端人员分离,使程序员改变程序的逻辑内容不会影响到前端...
smarty调用php常量
Php Smarty 中文版手册 最新版