PHP 提供多重处理日期格式的方法:使用 date() 函数将 Unix 时间戳转换为特定格式的字符串。使用 strtotime() 函数将人类可读的日期字符串转换为 Unix 时间戳。使用 date() 函数的字母代码自定义日期格式,如显示年份、月份、日期、小时、分钟和秒。

PHP 处理日期格式
PHP 提供了多种方法来处理日期格式。最常用的方法是使用 date() 函数和 strtotime() 函数。
date() 函数
date() 函数将 Unix 时间戳转换为指定格式的字符串。其语法如下:
立即学习“PHP免费学习笔记(深入)”;
<code class="php">date(format, timestamp)</code>
其中:
示例:
<code class="php">$timestamp = time();
$date_string = date("Y-m-d H:i:s", $timestamp);
echo $date_string; // 输出:2023-03-08 15:30:00</code>strtotime() 函数
strtotime() 函数将人类可读的日期字符串转换为 Unix 时间戳。其语法如下:
<code class="php">strtotime(string)</code>
其中:
示例:
<code class="php">$date_string = "2023-03-08 15:30:00"; $timestamp = strtotime($date_string); echo $timestamp; // 输出:1678293000</code>
自定义日期格式
使用 date() 函数,可以通过使用字母代码来指定自定义日期格式:
示例:
<code class="php">$timestamp = time();
$date_string = date("d/m/Y H:i", $timestamp);
echo $date_string; // 输出:08/03/2023 15:30</code>以上就是php如何处理日期格式的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号