duan0818 2013-04-03 05:04
浏览 27
已采纳

PHP +处理22:00至04:00之间的时间[关闭]

My first time on StackOverflow as a poster, normally just a browser, however I have run into an issue myself this time.

I have a script that sets certain variables based on what time it is.

<?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!';
}
}
?>

The script works fine, when it is for example 13:00, it checks between 10:00 & 14:00.

However, when the time is 23:30 and it checks between 22:00 & 02:00 it shows the "There is a barnacle!"

What I am assuming is that the jump from 22:00 into 02:00 confuses the script as 02:00 is less than 22:00 ?

Not the tidiest code or best method, but would like to know if anyone can suggest how I can overcome this?

展开全部

  • 写回答

4条回答 默认 最新

  • dongnanman9093 2013-04-03 05:09
    关注

    You could use an OR-condition for this special case:

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

    But as you already mentioned, your code is not very clean!

    Maybe this would be a little bit better:

    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部