举报投诉联系我们 手机版 热门标签 名动网
您的位置:名动网 > php strtotime函数 PHP strtotime() 函数

php strtotime函数 PHP strtotime() 函数

2023-03-07 22:20 PHP教程

php strtotime函数 PHP strtotime() 函数

php strtotime函数

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 strtotime() 函数

PHP strtotime() 函数

PHP Date/Time PHP Date/Time 参考手册

实例

将英文文本日期时间解析为 Unix 时间戳:

<?php
echo(strtotime("now") . "<br>");
echo(strtotime("3 October 2005") . "<br>");
echo(strtotime("+5 hours") . "<br>");
echo(strtotime("+1 week") . "<br>");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");
echo(strtotime("next Monday") . "<br>");
echo(strtotime("last Sunday"));
?>

运行实例 »

定义和用法

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() 函数。

语法

strtotime(time,now);

参数 描述
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 Date/Time 参考手册


阅读全文
以上是名动网为你收集整理的php strtotime函数 PHP strtotime() 函数全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
  • php array_fill PHP array_fill_keys() 函数

    php array_fill PHP array_fill_keys() 函数

    2023-03-18 PHP教程

    PHP array_fill_keys() 函数 完整的 PHP Array 参考手册 实例 用给定的指定键名的键值填充数组:?php$keys=array("a","b","c","d...

  • php use关键字 PHP MySQL Order By 关键词

    php use关键字 PHP MySQL Order By 关键词

    2023-03-07 PHP教程

    你可以对 MySQL 数据库中的记录集进行排序,具体请阅读本节内容。ORDER BY 关键词用于对记录集中的数据进行排序。ORDER BY 关键...

  • php array 函数 PHP 5 Array 函数

    php array 函数 PHP 5 Array 函数

    2023-03-14 PHP教程

    本章开始,主要介绍 PHP 函数中的常用函数,本节首先介绍 Array 函数!PHP Array 简介PHP Array 函数允许您访问并操作数组。支持...

  • php ftp函数 PHP FTP 函数

    php ftp函数 PHP FTP 函数

    2023-04-06 PHP教程

    本节主要介绍了 PHP 中 FTP 相关函数,讲解 php 如何操作 ftp 的连接、传输及文件与目录的相关操作的技巧。PHP FTP 简介 FTP 函...

  • php math PHP 5 Math 函数

    php math PHP 5 Math 函数

    2023-03-08 PHP教程

    通过 Math 函数能够处理 PHP 中的值,Math 函数包含许多用于计算的数学函数,本节将一一为你讲解!PHP Math 简介 Math 函数能处...

© 2024 名动网 mdwl.vip 版权所有 联系我们