duan0818 2013-04-03 13: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 13: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条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程