skip to Main Content

I’ve been racking my brain & starting then scratching code for this issue for a hot minute. So basically what I am looking to do is add one of three classes based on morning, afternoon or evening in only EST (New York). It seems simple enough, it really does, but I’m just not able to figure this one out. I’ve deleted more code than I care to mention. Any help would be vastly appreciated, I’m sure if you’re reading this you already know world’s more than me. Thank you.

2

Answers


  1. give that you have three different classes morning , afternoon and evening
    you can do as below

     <?php
                      
    
          //set timeZone to New York
    
          date_default_timezone_set("America/New_York");
    
          // get current time
          $current_date = date('H:i:s');
    
          // check morning slot you can change time slot according to yours in 24Hrs format
          if(strtotime($current_date)>=strtotime('06:00:00') and strtotime($current_date)<=strtotime('12:00:00') ){
               include "morning.class.php";
    
               $morning = new morning();
               // do your stuff
          }  
    
          // check noon slot you can change time slot according to yours in 24Hrs format
          if(strtotime($current_date)>=strtotime('12:00:01') and strtotime($current_date)<=strtotime('16:00:00') ){
               include "noon.class.php";
    
               $noon = new noon();
               // do your stuff
          } 
          
          // check evening slot you can change time slot according to yours in 24Hrs format
          if(strtotime($current_date)>=strtotime('16:00:01') and strtotime($current_date)<=strtotime('20:00:00') ){
               include "evening.class.php";
    
               $evening = new evening();
               // do your stuff
          } 
    
          //// you can add as much time slot you like depending on your requirement
    
    ?>
    

    Login or Signup to reply.
  2. I’m assuming you’re doing some kind of event or calendar view here.

    With that in mind you could define your time of days to correspond to a range of hours:

    $ranges = [
        'morning'=>[6, 11],
        'afternoon'=>[11, 17],
        'evening'=>[17,20]
    ];
    

    You would extract the hour from each records timestamp, ie:

    $hour = date('G', strtotime($result['timestamp']));
    

    Then you loop the ranges with the hour to get the time of day:

    $timeOfDayClass = '';
    foreach($ranges as $key=>$range) {
        if ($hour >= $range[0] && $hour < $range[1]) {
            $timeOfDayClass=$key;
            break;
        }
    }
    

    Put it all together in a function:

    echo timeOfDay('2012-12-18 13:23:19'); //afternoon
    
    function timeOfDay($timestamp) {
        
        $hour = date('G', strtotime($timestamp));
        
        $ranges = [
            'morning'=>[6, 11],
            'afternoon'=>[11, 17],
            'evening'=>[17,20]
        ];
    
        $timeOfDayClass = '';
        foreach($ranges as $key=>$range) {
            if ($hour >= $range[0] && $hour < $range[1]) {
                $timeOfDayClass=$key;
                break;
            }
        }
        return $timeOfDayClass;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search