【问题标题】:PHP If an Hour Is Between Two Other HoursPHP 如果一个小时在另外两个小时之间
【发布时间】:2014-10-15 00:15:16
【问题描述】:

我需要确定当前时间是否介于另外两个时间之间。例如,要检查时间是否在 07:00 到 10:00 之间,我可以使用:

$currentTime = new DateTime('09:00');
$startTime = new DateTime('07:00');
$endTime = new DateTime('10:00');

if ($currentTime->format('H:i') >= $startTime->format('H:i') && $currentTime->format('H:i') <= $endTime->format('H:i')) {
    // Do something
}

我遇到的问题是如果时间是 01:00 会发生什么,我想检查它是否在 22:00 和 07:00 之间。我并不担心这是另一天,只要它在 24 小时制的这两个小时之间。示例 01:00 介于 22:00 和 07:00 之间

22:00, 23:00, 00:00, 01:00, 02:00... 07:00

我最终想要实现的是,可以在不同时间为服务设置不同的价格。因此,我目前每小时循环计算该小时属于哪个价格范围并相应地改变价格。如果有人对此问题有更优雅的解决方案,我将不胜感激。

更新:

假设我有一条规则,规定在晚上 10 点到早上 7 点之间我要收取双倍费用。我从开始时间到结束时间每个小时循环,检查每个小时是否在 22:00(晚上 10 点)和 07:00(早上 7 点)之间,如果是,则应加倍收费。我想避免考虑日期。

【问题讨论】:

  • 但是,10:00 介于 20:0022:00 之间,因为 10:0022:00 被安排在第二天......从这个意义上说,任何时间都在两个给定时间之间.您的问题含糊不清,请更具体...
  • @CommuSoft 为清楚起见添加了更多信息,希望对您有所帮助。

标签: php datetime


【解决方案1】:
<?php
$currentTime = strtotime('1:00');
$startTime = strtotime('22:00');
$endTime = strtotime('7:00');

if (
        (
        $startTime < $endTime &&
        $currentTime >= $startTime &&
        $currentTime <= $endTime
        ) ||
        (
        $startTime > $endTime && (
        $currentTime >= $startTime ||
        $currentTime <= $endTime
        )
        )
) {
    echo 'open';
} else {
    echo 'clse';
}

【讨论】:

  • 适用于所有情况
  • 那是超级简单又超级合乎逻辑的!
  • 周末(周六和周日)关门怎么办?
【解决方案2】:

无需使用DateTime::format() 进行比较。 DateTime 对象已经具有可比性。

要处理跨越午夜的时间段,您需要更改日期,以便准确反映实际日期。

$currentTime = (new DateTime('01:00'))->modify('+1 day');
$startTime = new DateTime('22:00');
$endTime = (new DateTime('07:00'))->modify('+1 day');

if ($currentTime >= $startTime && $currentTime <= $endTime) {
    // Do something
}

【讨论】:

  • 要小心,因为(new DateTime('07:00')-&gt;X_MEMBER_METHOD() 不适用于所有 PHP 版本。注意对象实例化
  • 它适用于所有支持的版本。为了兼容性,我们只能向后走这么远。
  • @JohnConde 感谢关于不需要 format() 的提示,在实际程序中 currentTime 是一个日期/时间,因此为什么它在我的示例中以 format 出现,但我发现它在开始时不需要现在结束时间。谢谢。
  • 我明白你的意思。不是每个人都能使用最新的 PHP 版本。你只是很明显很固执,没有得到一点建设性的批评。没关系。
  • @self PHP 5.3 已经到了生命周期的尽头;所以假设 5.4 向前推进是完全可以接受的。
【解决方案3】:

我从第一个更流行的解决方案开始找到了另一种方法,该解决方案存在对我不起作用的问题。

我必须在晚上 10:00 到早上 6:00 之间写一些东西,但比较不起作用,因为当我过了午夜时,endTime 总是 > 开始时间。

所以如果我不介意我以这种方式解决的那一天:

$currentTime = new DateTime();
$startTime = new DateTime('22:00');
$endTime = (new DateTime('06:00'))->modify('+1 day');

if ($currentTime->format("a") == "am") { 
   // echo "It's a new day <br />"; 
   $currentTime = $currentTime->modify('+1 day');
}


if ($currentTime >= $startTime && $currentTime <= $endTime) {
    echo "hello";
}

【讨论】:

    【解决方案4】:
        //get current DateTime
        $date = new DateTime('now');
        $currentHour = 0;
        $currentMinutes = 0;
        foreach ($date as $item=>$value ) {
            if($item=="date"){
                $p = explode(" ",$value)[1];
                $hour = explode(":",$p);
                $currentHour = $hour[0];
                $currentMinutes = $hour[1];
            }
        }
        $returnVO["success"] = ((int)$currentHour<12 || ((int)$currentHour>=22 && (int)$currentMinutes > 0) ) ? false : true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 2018-03-29
      相关资源
      最近更新 更多