欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php将日期转为时间戳的方法

程序员文章站 2022-03-24 08:05:34
...

php将日期转为时间戳的方法:首先创建一个PHP示例文件;然后直接使用strtotime函数语法语句如“strtotime('2010-03-24 08:15:42');”来将日期转为时间戳即可。

php将日期转为时间戳的方法

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

日期转换为UNIX时间戳用函数:strtotime()

一般形式:strtotime('2010-03-24 08:15:42');

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);

实例

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

<?php
echo(strtotime("now") . "<br>");
echo(strtotime("15 October 1980") . "<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"));
?>

输出:

1620696264
1473004800
1620714264
1621301064
1621585469
1621180800
1620489600

推荐学习:《PHP视频教程

以上就是php将日期转为时间戳的方法的详细内容,更多请关注其它相关文章!

相关标签: php