php strtotime函数怎么用
strtotime()函数是PHP中的一个内置函数,用于将英文文本时间或日期描述转换为UNIX时间戳。下面本篇文章就来给大家介绍一下strtotime()函数的使用方法,希望对大家有所帮助。

PHP strtotime()函数怎么用?
strtotime()函数接受表示日期时间的字符串参数,将任何英文文本的日期或时间描述解析为 Unix 时间戳;例如,“now”指的是当前日期。该函数返回自Unix Epoch以来的秒数。我们还可以使用date()函数以日期格式返回英文文本日期时间。
基本语法:
strtotime ($EnglishDateTime, $time_now)
参数:strtotime()函数接受两个参数
● $EnglishDateTime :指定英文文本时间或日期描述,表示要返回的日期或时间。该函数解析字符串并以秒为单位返回时间;不可省略。
● $time_now:指定用于计算返回值的时间戳;可省略。
注:由于时间/日期不是静态的,因此输出会有所不同。
下面通过代码示例来看看PHP strtotime()函数的使用
示例1:显示当前时间
<?php
header("content-type:text/html;charset=utf-8");
//以秒为单位显示当前时间
echo "以秒为单位显示当前时间:".strtotime("now"), "<br>";
// 以日期格式显示当前时间
echo "以日期格式显示当前时间:".date("Y-m-d", strtotime("now"))."\n";
?>
输出:
以秒为单位显示当前时间:1556162013
以日期格式显示当前时间:2019-04-25
示例2:显示“12th february 2017”所描述的时间日期
<?php
header("content-type:text/html;charset=utf-8");
// 以秒为单位显示转换后的日期时间
echo "以秒为单位显示:".strtotime("12th february 2017")."<br>";
// 以日期格式显示转换后的日期时间
echo "以日期格式显示:".date("Y-m-d", strtotime("12th february 2017"))."<br>";
?>
输出:
以秒为单位显示:1486854000
以日期格式显示:2017-02-12
示例3:显示当前时间的下周日的日期
<?php
header("content-type:text/html;charset=utf-8");
// 以秒为单位显示转换后的日期时间
echo "以秒为单位显示:".strtotime("next sunday")."<br>";
// 以日期格式显示转换后的日期时间
echo "以日期格式显示:".date("Y-m-d", strtotime("next sunday"))."<br>";
?>
输出:
以秒为单位显示:1556402400
以日期格式显示:2019-04-28
相关推荐
-
MySQL 安装失败,提示Apply Security Settings 的处理办法
MySQL 安装失败,提示Apply Security Settings 的处理办法2025-04-20 01:54:57 -
MySQL事务隔离级别详解2025-04-20 01:44:01
-
一文说清nginx规则匹配(含案例分析)2025-04-20 01:10:02
-
运维服务篇:Nginx常用功能(rewrite重定向/location定位等)
运维服务篇:Nginx常用功能(rewrite重定向/location定位等)2025-04-20 00:55:25 -
php定义变量规则不能包含哪些字符?2025-04-20 00:27:24