2013-04-30 124 views
0
$currentTime = date("Hi"); 

if($currentTime > 0559 && $currentTime < 1401) { 
    // Stuff here 
} 

这是我当前的代码,但if语句似乎在午夜后的任何时间运行,而不是在0600(当地时间上午6:00)之后运行。任何想法会造成这种情况。如何解释if/else PHP语句中的时间?

+0

如果你用'0'开始一个整数字面值,它会被解释为八进制。 – 2013-04-30 17:27:38

+0

但自date()返回一个字符串,你可以使用$ currentTime>'0559',它应该工作... – jcorry 2013-04-30 17:29:52

+0

有趣的是,'0559'实际上等于'45':'9'被抛出,作为这个数字显然不能用于十进制数字。所以,确切地说,剧本在00:45以后的任何时候都可以工作,而不是在午夜之后的任何时候。 ) – raina77ow 2013-04-30 17:30:04

回答

3

(if($currentTime > 0559) - 0559将被视为octal,因为之前的0

只需拆除,在比较if($currentTime > 559)

+0

太好了。谢谢!我知道这将是一件愚蠢的事情。 – 2013-04-30 17:31:36

1

确保您使用的整数。

$currentTime = intval(date("Hi")); 

if ($currentTime > 559 && $currentTime < 1401) { 
    // Stuff here 
} 
2

您正在使用的是什么,你认为你正在使用其他方式的整数,请integer文档here

例如

$a = 0123; // octal number (equivalent to 83 decimal) 

让我们回到你的代码,修正它只是在您的整数之前删除0,因此它不会被解释为octal,只需更改为

if(($currentTime > 559) && ($currentTime < 1401)) { 
    // Stuff here 
}