赣南脐橙

佰草伐

导航

php strtotime()函数将日期和时间解析为UNIX时间戳

在PHP中我们使用strotime()函数可以将任何英文文本的日期和时间解析为UNIX时间戳。它的语法如下:大理石平台精度等级

 

strotime()函数语法格式

1

strtotime(time,now);

其值是相对于参数now给出的时间,如果没有提供此参数now则用系统当前时间。

参数详解:

该函数有两个参数

time

被解析的字符串,格式根据 GNU » 日期输入格式 的语法。在 PHP 5.0 之前,time 中不允许有毫秒数,自 PHP 5.0 起可以有但是会被忽略掉。

now

用来计算返回值的时间戳。 该参数默认值是当前时间time(),也可以设置为其他时间的时间戳()

返回值: 成功则返回间戳,否则返回 FALSE 。在 PHP 5.1.0 之前本函数在失败时返回 -1,后面版本返回false.

strtotime的第一个参数可以是我们常见的英文时间格式,比如“2008-8-20”或“10 September 2000 ”等等。也可以是以参数now为基准的时间描述,比如“+1 day”等等。

strotime()函数实例

实例一

使用strotime()函数获取指定日期的unix时间戳,代码如下

1

2

3

4

<?php

echo strtotime("2017-4-10");

 

?>

运行结果:

 

实例说明:返回2017年4月10日0点0分0秒时间戳

 

实例二

本实例应用strotime()函数获取英文格式日期时间字符串的UNIX时间戳,并将部分时间输出,实例代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?php

header("Content-type:text/html;charset=utf-8");    //设置编码

 

echo strtotime("now")." ";                                              //当前时间的时间戳

echo "输出时间:".date("Y-m-d H:i:s",strtotime("now"))."<br/>";        //输出当前时间

 

 

echo strtotime("10 May 2017")." ";                                     //输出指定日期的时间戳

echo "输出时间:".date("Y-m-d H:i:s",strtotime("10 May 2017"))."<br/>";        //输出指定日期的时间

 

 

echo strtotime("+3 day")." ";                                           //输出当前时间三天后的时间戳

echo "输入三天后的时间:".date("Y-m-d H:i:s",strtotime("+3 day"))."<br/>";

 

echo strtotime("+1 week")." ";

echo "输出下个星期此时的时间:".date("Y-m-d H:i:s",strtotime("+1 week"))."<br/>";

 

echo strtotime("+1 week 2 days 3 hours 4 seconds")."<br/>";

 

echo strtotime("NEXT Thursday")."<br/>";

 

echo strtotime("last Monday");

 

?>

posted on 2019-12-30 16:28  佰草伐  阅读(632)  评论(0编辑  收藏  举报

自定义导航网站

php基础知识

Wood Furniture