PHP检查时间是否在范围内,质疑通用解决方案 [英] PHP check if time falls within range, questioning common solution

查看:84
本文介绍了PHP检查时间是否在范围内,质疑通用解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须检查当前的白天是否在特定范围内.我上网查找了类似的几种解决方案:

I have to check if the current daytime falls in a specific range. I looked up the internet and found several similar solutions like this one:

$now = date("His");//or date("H:i:s")

$start = '130000';//or '13:00:00'
$end = '170000';//or '17:00:00'

if($now >= $start && $now <= $end){
echo "Time in between";
}
else{
echo "Time outside constraints";
}

如果两个条件都必须成立,那么当我们假设$ start为06:00:00和$ end为02:00:00时,如何达到这个条件.

If both conditions have to be true, how can this bis achieved when we assume that $start is 06:00:00 and $end is 02:00:00.

如果我们假设它是01:00:00,那么在这种情况下第一个条件就不能成立.

If we make the assumption that it is 01:00:00, in this case the first condition can't be true.

有人有不同的想法来解决这个问题吗?

Has anybody an idea to handle this problem differently?

谢谢!

推荐答案

自然,您必须在比较中考虑日期.

Naturally, you'd have to account for date in your comparisons.

<?php

$start = strtotime('2014-11-17 06:00:00');
$end = strtotime('2014-11-18 02:00:00');

if(time() >= $start && time() <= $end) {
  // ok
} else {
  // not ok
}

这篇关于PHP检查时间是否在范围内,质疑通用解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆