PHP strtotime() 函数用于将任何英文文本的日期/时间转换为 Unix 时间戳。
Unix 时间戳是一个整数,它表示从格林威治时间 1970 年 1 月 1 日 00:00:00 起至今的秒数。
echo strtotime("now"); // 输出当前 Unix 时间戳 echo strtotime("10 September 2000"); // 输出 Unix 时间戳 2000-09-10 00:00:00 echo strtotime("+1 day"); // 输出明天的 Unix 时间戳 echo strtotime("+1 week"); // 输出一周后的 Unix 时间戳 echo strtotime("+1 week 2 days 4 hours 2 seconds"); // 输出一周零两天四小时零两秒后的 Unix 时间戳 echo strtotime("next Thursday"); // 输出下周四的 Unix 时间戳 echo strtotime("last Monday"); // 输出上周一的 Unix 时间戳
strtotime() 函数可以识别大多数英文文本,例如:
echo strtotime("now"), "\n"; // 现在的日期和时间(Unix时间戳) echo strtotime("10 September 2000"), "\n"; // 2000年9月10号 echo strtotime("+1 day"), "\n"; // 加一天 echo strtotime("+1 week"), "\n"; // 加一星期 echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n"; // 加一星期、两天、四小时、二秒 echo strtotime("next Thursday"), "\n"; // 下周四 echo strtotime("last Monday"), "\n"; // 上周一
strtotime() 函数还可以识别具体的日期和时间格式:
echo date('Y-m-d H:i:s',strtotime('2020-02-20 12:30')); // 2020-02-20 12:30:00
strtotime() 函数还可以识别具体的星期格式:
echo date('Y-m-d H:i:s',strtotime('Monday')); // 本周一
PHP Date/Time 参考手册
将英文文本日期时间解析为 Unix 时间戳:
strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。
注意:如果年份表示使用两位数格式,则值 0-69 会映射为 2000-2069,值 70-100 会映射为 1970-2000。
注意:请注意 m/d/y 或 d-m-y 格式的日期,如果分隔符是斜线(/),则使用美洲的 m/d/y 格式。如果分隔符是横杠(-)或者点(.),则使用欧洲的 d-m-y 格式。为了避免潜在的错误,您应该尽可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函数。
参数 | 描述 |
---|---|
time | 必需。规定日期/时间字符串。 |
now | 可选。规定用来计算返回值的时间戳。如果省略该参数,则使用当前时间。 |
返回值: | 成功则返回时间戳,失败则返回 FALSE。 |
---|---|
PHP 版本: | 4+ |
更新日志: | PHP 5.3.0:现在相对时间格式,比如这一周、前一周、上一周、下一周,规定一周从星期一到星期日,而不是使用相对于当前日期/时间的前后 7 天。 PHP 5.3.0: 现在 24:00 是有效的格式。 PHP 5.2.7:在这之前的版本中,如果请求一个月中某个给定日期且该日期刚好是该月的第一天,则会错误地把增加一个星期到返回的时间戳中,这点现在已经得到更正。 PHP 5.1.0:如果失败则返回 FALSE(在这之前的版本则返回 -1),并增加了 E_STRICT 和 E_NOTICE 时区错误。 PHP 5.0.2:现在正确计算 "now" 和其他相对时间是以当前时间为准,而不是以今天午夜的时间为准。 PHP 5.0.0:允许微秒(但微秒数通常被忽略)。 |
Unix时间戳(timestamp)转换工具
PHP Date/Time 参考手册
PHP array_fill_keys() 函数 完整的 PHP Array 参考手册 实例 用给定的指定键名的键值填充数组:?php$keys=array("a","b","c","d...
你可以对 MySQL 数据库中的记录集进行排序,具体请阅读本节内容。ORDER BY 关键词用于对记录集中的数据进行排序。ORDER BY 关键...
本章开始,主要介绍 PHP 函数中的常用函数,本节首先介绍 Array 函数!PHP Array 简介PHP Array 函数允许您访问并操作数组。支持...
本节主要介绍了 PHP 中 FTP 相关函数,讲解 php 如何操作 ftp 的连接、传输及文件与目录的相关操作的技巧。PHP FTP 简介 FTP 函...
通过 Math 函数能够处理 PHP 中的值,Math 函数包含许多用于计算的数学函数,本节将一一为你讲解!PHP Math 简介 Math 函数能处...