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?

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;

You should convert your times with mktime and after that you can check if a time is greater than another, check http://php.net/manual/en/function.mktime.php

Just a wild guess. Have you tried to move last condition (elseif) before 22:00<->2:00 condition, and make your 22:00 condition only with one cond -> only >22:00 (without <2:00)

When working with dates you should always use a object to help you like DateTime. Those object will help you to manipulate your dates and to get the Timestamp. Using timestamp is always a idea when you compare dates.

Hope it helped.