【问题标题】:PHP + Dealing with time between 22:00 and 04:00 [closed]PHP +处理22:00到04:00之间的时间[关闭]
【发布时间】:2013-04-03 13:04:22
【问题描述】:

我第一次作为海报使用 StackOverflow,通常只是一个浏览器,但是这次我自己遇到了一个问题。

我有一个脚本可以根据现在的时间设置某些变量。

<?php
// Sunday
if( in_array($day, array(Sun)) ){
echo 'Conditions = Day:' . $day . ' ' . 'Time: ' . $current_time;
if (($current_time >= '06:01') && ($current_time <= '10:00')) {
    $stime = '06:00';
    $etime = '10:00';
    $showname = 'Dimitri Jegels';
 $image = 'images/placeholder/on-air.jpg';
//  echo $stime . ' and' . $etime . ' & ' . $showname . ' & ' . $image;
} elseif (($current_time >= '10:01') && ($current_time <= '14:00')) {
    $stime = '10:00';
    $etime = '14:00';
    $showname = 'Benito Vergotine';
 $image = 'images/placeholder/on-air.jpg';
//  echo $stime . ' and' . $etime . ' & ' . $showname . ' & ' . $image;
} elseif (($current_time >= '14:01') && ($current_time <= '18:00')) {
    $stime = '14:00';
    $etime = '18:00';
    $showname = 'Gavin Arends';
 $image = 'images/placeholder/on-air.jpg';
//  echo $stime . ' and' . $etime . ' & ' . $showname . ' & ' . $image;
} elseif (($current_time >= '18:01') && ($current_time <= '22:00')) {
    $stime = '18:00';
    $etime = '22:00';
    $showname = 'Tracy Lange';
 $image = 'images/placeholder/on-air.jpg';
//  echo $stime . ' and' . $etime . ' & ' . $showname . ' & ' . $image;
} elseif (($current_time >= '22:01') && ($current_time <= '02:00')) {
    $stime = '22:00';
    $etime = '02:00';
    $showname = 'Mehboob Bawa';
 $image = 'images/placeholder/on-air.jpg';
//  echo $stime . ' and' . $etime . ' & ' . $showname . ' & ' . $image;
} elseif (($current_time >= '02:01') && ($current_time <= '06:00')) {
    $stime = '02:00';
    $etime = '06:00';
    $showname = 'Training Slot';
 $image = 'images/placeholder/on-air.jpg';
//  echo $stime . ' and' . $etime . ' & ' . $showname . ' & ' . $image;
} else {
    $stime = '??:??';
    $etime = '??:??';
    $showname = 'There is a barnacle!';
}
}
?>

脚本运行良好,例如 13:00 时,它会在 10:00 和 14:00 之间进行检查。

但是,当时间是 23:30 并且在 22:00 和 02:00 之间检查时,它会显示“有藤壶!”

我假设从 22:00 跳转到 02:00 会使脚本混淆,因为 02:00 小于 22:00?

不是最整洁的代码或最好的方法,但想知道是否有人可以建议我如何克服这个问题?

【问题讨论】:

    标签: php date if-statement time


    【解决方案1】:

    对于这种特殊情况,您可以使用 OR 条件:

    } elseif (($current_time >= '22:01') || ($current_time <= '02:00')) {
    

    但是正如你已经提到的,你的代码不是很干净!

    也许这样会好一点:

    $names = array('Mehboob Bawa', 'Training Slot', 'Dimitri Jegels', 'Benito Vergotine', 'Gavin Arends', 'Tracy Lange');
    $hours = getdate()["hours"];
    $interval = (int)((($hours + 2) % 24) / 4);
    $showname = $name[$interval];
    $image = 'images/placeholder/on-air.jpg'; // can be also done via an array, if not always the same
    $stime = ($interval * 4 + 22) % 24;
    $etime = ($interval * 4 + 2) % 24;
    

    【讨论】:

      【解决方案2】:

      在处理日期时,您应该始终使用对象来帮助您喜欢DateTime。这些对象将帮助您操作日期并获取时间戳。当您比较日期时,使用时间戳总是一个想法。

      希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        你应该用 mktime 转换你的时间,然后你可以检查一个时间是否大于另一个,检查http://php.net/manual/en/function.mktime.php

        【讨论】:

          【解决方案4】:

          只是一个疯狂的猜测。您是否尝试过在 22:002:00 条件之前移动最后一个条件(elseif),并仅在一个条件下使您的 22:00 条件 -> 仅 >22:00(没有

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多