比较php

时间:2016-09-01 21:39:22

标签: php if-statement time conditional strtotime

场景是,我有一家商店,晚上1点开门,晚上10点关门。 对于任何当前时间我只想检查该时间戳是否位于商店开放和关闭时间之间。

Yeap那很简单,我仍然不知道为什么,我觉得很难。 下面是我正在尝试的史诗剧。

<?php

$t=time();  //for current time
$o = date("Y-m-d ",$t)."01:00:00"; //store open at this time
$c = date("Y-m-d ",$t)."22:00:00"; //store closes at this time

//Yes, its crazy .. but I love echoing variables 
echo "current time = ".$t;
echo PHP_EOL;
echo "Start = ".strtotime($o);
echo PHP_EOL;
echo "End = ".strtotime($c);
echo PHP_EOL;

// below condition runs well, $t is always greater than $o
if($t>$o){
  echo "Open_c1";
}else{
  echo "Close_c1";
}

//Here's where my variable $t, behaves like a little terrorist and proclaims itself greater than $c
if($t<$c){
    echo "Open_c2";
}else{
    echo "Close_c2";
}
?>

输出:在phpfiddle上

当前时间= 1472765602开始= 1472706000结束= 1472781600 Open_c1 Close_c2

只需一个帮助,为什么($ t&lt; $ c)条件为假。 我错过了一些非常普遍或犯了严重错误的事情。

谢谢。

2 个答案:

答案 0 :(得分:2)

试试这个

$o = date("Y-m-d 01:00:00"); //store open at this time
$c = date("Y-m-d 22:00:00"); //store closes at this time

答案 1 :(得分:2)

那是因为$o$c是字符串而$ t是时间(); 您需要将ifs更改为

 if ($t < strtotime($c))

 if ($t > stttotime($o))

让它正常工作。