skip to Main Content

I want to run my code from 21:00 to 08:00, for example. But my code does not work properly because when the clock reaches 24:00 (00:00), my code gets confused and cannot set 01:00. check 00:00 because it is a 24-hour clock.

I need this code for the dark mode of the site, so that my site will be in dark mode between a desired hour, for example, from 10 pm to 6 am.

Note: I may specify any hour for this task and I do not choose just one hour. For example, I may choose 21:00 to 3:00 am. It is random because the hours are chosen by my clients and I do not know what hours they choose. !!!

Can you guide me to make my code work correctly in 24 hours if I specify any hour?


    $hour = date('H');
    //$hour = 23;

    if( $hour > 22 && $hour < 8) {

      echo "Good night";

    }else{
      echo "Good Morning";
    }

2

Answers


  1. You can use the modulo operator to handle the 24-hour clock.

    $start_hour = 21;
    $end_hour = 8;
    $current_hour = date('H');
    
    // handle the 24-hour cycle
    $current_hour = $current_hour % 24;
    
    if($current_hour >= $start_hour || $current_hour < $end_hour) {
        echo "Good night";
    } else {
        echo "Good morning";
    }
    

    This way it will work for any hours range you choose, as long as the start hour is greater than or equal to the end hour.

    For HH:MM:

    $start_time = "21:00";
    $end_time = "08:00";
    $current_time = date('H:i');
    
    $start_hour = (int)substr($start_time, 0, 2);
    $start_minute = (int)substr($start_time, 3, 2);
    
    $end_hour = (int)substr($end_time, 0, 2);
    $end_minute = (int)substr($end_time, 3, 2);
    
    $current_hour = (int)substr($current_time, 0, 2);
    $current_minute = (int)substr($current_time, 3, 2);
    
    // handle the 24-hour cycle
    $current_hour = $current_hour % 24;
    
    if(($current_hour > $start_hour || ($current_hour == $start_hour && $current_minute >= $start_minute)) || ($current_hour < $end_hour || ($current_hour == $end_hour && $current_minute < $end_minute))) {
        echo "Good night";
    } else {
        echo "Good morning";
    }
    
    Login or Signup to reply.
  2. When you reverse the intervals (21:00 to 08:00 vs 08:00 to 21:00) you also need to reverse the logic:

    • If $start < $end, you need to check $current >= $start && $current <= $end
    • Otherwise, $current >= $start || $current <= $end

    You’ll also want to consider minutes and seconds as well. For that, you can pass a full properly formatted H:M:S string and convert everything to the same unit so comparisons make sense. It’s easier to see if you create a dedicated functions that do only one thing:

    // Error checking omitted for brevity
    function hmsToSeconds(string $hms): int
    {
        [$h, $m, $s] = array_pad(preg_split('/[^d]+/', $hms), 3, 0);
        return 3600 * $h + 60 * $m + $s;
    }
    
    function inRange(string $start, string $end, string $current = null): bool
    {
        if ($current === null) {
            $current = date('H:i:s');
        }
    
        $start = hmsToSeconds($start);
        $end = hmsToSeconds($end);
        $current = hmsToSeconds($current);
    
        if ($start < $end) {
            return $current >= $start && $current <= $end;
        }
        return $current >= $start || $current <= $end;
    }
    
    if (inRange('21:00:00', '8:00:00')) {
        echo 'Good night';
    } else {
        echo 'Good morning';
    }
    

    Demo

    Needless to say, since we’re doing this on the server we’re using server clock, so it may or may nor make sense for the user. If you know the user’s time zone, you can take it into account when calculating current time.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search