当前位置: 首页 > 编程文章 >

PHP Smarty date_format 时间日期格式化详解

PHP Smarty date_format 时间日期格式化详解

作者:顾禾时间:2026-06-26 14:17:49浏览:141

Smarty 模板中的 date_format 修饰符用于将 Unix 时间戳格式化为指定字符串。该修饰符本质上是 PHP strftime() 函数的封装,支持通过百分号前缀的转换说明符自定义输出格式,如星期、月份、12/24小时制等。由于底层依赖系统环境,具体可用的转换符可能因编译配置而异,建议参考系统手册确认完整列表。

本文目录收起

Smarty date_format 基础用法与示例

示例 5-8:date_format(日期格式)


index.php:


$smarty = new Smarty;


$smarty->assign('yesterday', strtotime('-1 day'));


$smarty->display('index.tpl');


index.tpl:


{$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"}


输出结果:


Feb 6, 2001


Tuesday, February 6, 2001


:33:00


Feb 5, 2001


Monday, February 5, 2001


:33:00


示例 5-9:date_format 转换说明符


%a - abbreviated weekday name according to the current locale


(根据当地格式输出“星期”缩写格式)


%A - full weekday name according to the current locale


(根据当地格式输出“星期”全称格式)


%b - abbreviated month name according to the current locale


(根据当地格式输出“月”缩写格式)


%B - full month name according to the current locale


(根据当地格式输出“月”全称格式)


%c - preferred date and time representation for the current locale


%C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)


%d - day of the month as a decimal number (range 00 to 31)


%D - same as %m/%d/%y


%e - day of the month as a decimal number, a single digit is preceded by a


space (range 1 to 31)


%g - Week-based year within century [00,99]


%G - Week-based year, including the century [0000,9999]


%h - same as %b


%H - hour as a decimal number using a 24-hour clock (range 00 to 23)


%I - hour as a decimal number using a 12-hour clock (range 01 to 12)


%j - day of the year as a decimal number (range 001 to 366)


%k - Hour (24-hour clock) single digits are preceded by a blank. (range 0 to 23)


%l - hour as a decimal number using a 12-hour clock, single digits preceeded by


a space (range 1 to 12)


%m - month as a decimal number (range 01 to 12)


%M - minute as a decimal number


%n - newline character


%p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale


%r - time in a.m. and p.m. notation


%R - time in 24 hour notation


%S - second as a decimal number


%t - tab character


%T - current time, equal to %H:%M:%S


%u - weekday as a decimal number [1,7], with 1 representing Monday


%U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week


%V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1


is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.


%w - day of the week as a decimal, Sunday being 0


%W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week


%x - preferred date representation for the current locale without the time


%X - preferred time representation for the current locale without the date


%y - year as a decimal number without a century (range 00 to 99)


%Y - year as a decimal number including the century


%Z - time zone or name or abbreviation


%% - a literal `%' character


PROGRAMMERS NOTE: date_format is essentially a wrapper to PHP's strftime()


function. You may have more or less conversion specifiers available depending


on your system's strftime() function where PHP was compiled. Check your


system's manpage for a full list of valid specifiers.


程序员提示:date_format本质上是php的strftime()函数的一个包装。


当php被编译的时候你可以或多或少的依靠系统的strftime()转换有效的区分符。


可以查看系统手册的有效区分符的全表.

上一篇
PHP文件上传漏洞利用与绕过技巧详解

文件上传漏洞允许攻击者上传恶意脚本以获取服务器权限,危害极大。本文详细解析了五种常见的防御场景及其对应的绕过技巧。包括前端JS白名单、后端Content-Type检查、黑名单过滤、扩大黑名单以及复合判断逻辑。针对每种场景,介绍了如BurpSuite抓包改包、.htaccess配置解析、大小写混淆、Windows系统特性(空格/点截断、::$DATA流)、双写替换、%00截断、%0a换行符匹配、图片马制作、二次渲染插入及条件竞争等多种具体绕过手段,帮助理解漏洞原理与防御盲区。

下一篇
PHP 使用 Expat 解析 XML:NewsBoy 类实现详解

本文通过 NewsBoy 类的源码,深入解析 PHP 中如何使用 Expat 扩展处理 XML 文档。文章详细展示了如何定义标记数组、注册 startElement、endElement 和 characterData 等回调函数,以及利用 xml_parser_create 和 xml_set_object 将分析器绑定到对象实例。同时介绍了大小写折叠选项的设置及文件读取流程,为构建轻量级 XML 处理组件提供具体参考。

相关文章

更多
Smarty日期时间格式化与选择控件用法详解

本文介绍Smarty模板引擎中处理日期时间的两种主要方式。首先讲解如何利用smarty.now变量配合date_format修饰符,通过%Y、%m等占位符实现自定义格式的日期输出。其次详细说明html_select_date函数生成下拉菜单的属性配置,包括prefix、start_year、field_order等参数,并指出该函数day与month不关联的缺陷,最后提供一段JavaScript代码用于动态修正天数显示。

2026.01.17 574
PHP Smarty模板引擎详细语法与配置指南

本文详细介绍PHP Smarty模板引擎的核心机制,包括定界符定制、注释与变量引用规则。重点解析{$smarty}保留变量的各类访问方式,如页面请求、时间戳及常量获取。同时系统梳理capitalize、date_format等常用变量调节器的参数用法,以及capture、foreach、if等内建函数的逻辑控制与数据输出功能,最后提供完整的Smarty实例化与路径配置代码示例。

2026.02.20 532
PHP日期格式统一与时间戳计算技巧

本文总结了PHP中两个实用的日期操作技巧。首先介绍如何利用checkdate和mktime函数将非标准格式的日期(如2026-9-9)转换为数据库通用的Y-m-d格式,确保存储一致性。其次讲解如何避免手动计算日期时遇到的月末、闰月等复杂逻辑,通过strtotime("-1 day")直接获取上一天的开始和结束时间戳,利用PHP内置算法自动处理月份天数差异。

2026.04.26 988
PHP封装的SmartyBC类完整实例

本文提供PHP环境下对SmartyBC模板引擎进行封装的完整代码实例。该实现旨在简化Smarty在PHP项目中的集成流程,通过封装类屏蔽底层配置细节,便于开发者快速调用模板渲染功能。内容涵盖核心类的定义及基本使用方式,可作为基于Smarty模板的PHP程序设计的参考素材。

2026.11.25 984
Smarty模板中获取与格式化当前日期时间的方法

在Smarty模板中无法直接使用PHP的date函数,需通过{$smarty.now}获取当前Unix时间戳。利用内置的date_format修饰符可将时间戳转换为指定格式,其语法与PHP的strftime()函数一致。例如使用{%Y-%m-%d %H:%M:%S%}可输出标准日期时间字符串,支持对模板变量或当前时间进行灵活格式化。

2026.07.29 949
PHP使用date()函数获取并格式化当前日期时间详解

在PHP开发中,利用内置的date()函数可以便捷地获取当前系统时间并进行自定义格式化输出。该函数通过特定的格式字符(如Y、m、d、H、i、s等)来定义年、月、日、时、分、秒的显示样式,支持12小时制与24小时制切换,以及星期和月份的英文全称或缩写形式。掌握这些参数组合,能够灵活生成符合业务需求的日期时间字符串。

2026.08.18 947

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。