【问题标题】:How would I interpret time in an if/else PHP statement?我将如何解释 if/else PHP 语句中的时间?
【发布时间】:2013-04-30 17:24:10
【问题描述】:
$currentTime = date("Hi");

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

这是我当前的代码,但 if 语句似乎在午夜之后的任何时间运行,而不仅仅是在 0600(当地时间上午 6:00)之后。任何想法会导致这种情况。

【问题讨论】:

  • 如果您以 0 开头的整数文字,它会被解释为八进制。
  • 但是由于 date() 返回一个字符串,你可以使用 $currentTime > '0559' 它应该可以工作......
  • 有趣的是0559实际上等于459被抛弃了,因为这个数字显然不能用于八进制。因此,准确地说,脚本在00:45 之后的任何时间都可以工作,而不是any time after midnight。 )

标签: php codeigniter if-statement time


【解决方案1】:

(if($currentTime &gt; 0559) - 0559 将被视为octal,因为前面有0

在比较中删除它if($currentTime &gt; 559)

【讨论】:

  • 太棒了。谢谢!我知道这会是一件愚蠢的事情。
【解决方案2】:

您正在以另一种方式使用整数,请查看integer 文档here

示例

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

让我们回到您的代码,要修复它,只需删除整数之前的 0,这样它就不会被解释为 octal,只需更改为

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

【讨论】:

    【解决方案3】:

    确保您使用的是整数。

    $currentTime = intval( date("Hi") );
    
    if ($currentTime > 559 && $currentTime < 1401) {
        // Stuff here
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      相关资源
      最近更新 更多