php strtotime,PHP strtotime()函数

strtotime函数在php中是可以把日志转换成时间戳,他可以方便的让我们对日期时间分秒的计算,下面我来给各位介绍时strtotime函数用法,有需要的同学可参考。

1、获取当前的时间的时间戳!

a.实用strtotime('now');来获取当前的时间戳!(因为PHP当前时间和真是时间相差8小时)

1 echo date('Y-m-d H:i:s',strtotime('now')+8*60*60);

结果:2019-06-14 10:10:02

b.你也可以用time() 来直接获取!

1 echo date('Y-m-d H:i:s',time()+8*60*60);

结果:2019-06-14 10:10:02

2、获取10天之后的时间戳:

使用方法:

1 strtotime("+10 days")+8*60*60;

输出10天后的日期,便于查看。

1 echo date('Y-m-d H:i:s',strtotime("+10 days")+8*60*60);

结果:2019-06-24 10:10:02

3、获取一周的时间戳:

使用方法:

1 strtotime("+1 week")+8*60*60;

或者你使用上面按天来获取时间戳.写法如:

strtotime("+7 days");

4、还可以获取指定几周几天几小时几秒来获取时间戳

使用方法:

strtotime ("+1 week 2 days 4 hours 2 seconds")+8*60*60;

echo date('Y-m-d H:i:s',strtotime ("+1 week 2 days 4 hours 2 seconds")+8*60*60);

结果:2019-06-23 14:04:13;

5、获取上周一或者下周四的时间戳:

使用方法:

a、获取上周一的时间戳:

strtotime ("last Monday")+8*60*60;

echo date('Y-m-d H:i:s',strtotime ("last Monday")+8*60*60);

结果:2019-06-10 08:00:00;

b.获取下周四的时间戳

strtotime ("next Thursday")+8*60*60;

echo date('Y-m-d H:i:s',strtotime ("next Thursday")+8*60*60);

结果:2019-06-20 08:00:00;

strtotime函数比较两个时间的大小

strtotime()函数对两个固定的时间进行比较,如下:

1、定义两个固定的时间;

2、通过strtotime()函数将固定时间转换成时间戳;

3、对两个时间戳的值进行比较。

例子:

$time="2019年05月23日15时50时20秒";

$times="2019-06-14 10:10:10";

if(strtotime($time)-strtotime($times)<0){

echo "时间:".$time." 早于时间:".$times;

}else{

echo "时间:".$times." 早于时间:".$time;

}

echo "

它们相差的时间值是:".(strtotime($time)-strtotime($times));

?>

上面的代码运行结果如下:

时间:2019年05月23日15时50时20秒 早于时间:2019-06-14 10:10:10

它们相差的时间值是:-1560507010

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值