coryetzkorn / php-store-hours

Output content based on time-of-day and-day-of-week.
MIT License
136 stars 55 forks source link

Store open after midnight #7

Closed braininteractive closed 8 years ago

braininteractive commented 9 years ago

Hi, thanks for this code very useful and good way to handle the opening hours, but I am still having a issue after midnight..

If we set the config like the documentation it wont work.. 'thu' => array('11:00-1:30'), // Open late 'fri' => array('11:00-20:30'),

Here is the problem when I am in Thursday late (Friday early morning 1:00am) the function is_open consider I am in Friday and go to check the opening time of Friday but never go to check the day before. On this case it's true Friday is close but I am still open with Thursday config until 1:30am (Friday)

Solutions :

  1. Update the documentation 'thu' => array('11:00-00:00'), // Open late 'fri' => array('00:00-1:30','11:00-20:30')
2. Or check the yesterday date with something like this : 
public function hours_yesterday() {
    $yesterday = strtotime("yesterday midnight");
    $yday = strtolower(date('D',strtotime("-1 days")));
    $exceptions = $this->exceptions;
    $hours_yesterday = $this->hours[$yday];
    if($exceptions) {
        foreach($exceptions as $ex_day => $ex_hours) {
            //echo 'ex' . date('r', strtotime($ex_day + '/2014'));
            //echo 'today' . date('r', $today);
            if(strtotime($ex_day) == $yesterday) {
                // Today is an exception, use alternate hours instead
                $hours_yesterday = $ex_hours;
            }
        }
    }
    return $hours_yesterday;
}

// Returns boolean public function is_open() { // today // bla bla bla

// yesterday
 $hours_yesterday = $this->hours_yesterday();
   if(!empty($hours_yesterday[0])) {
      foreach($hours_yesterday as $range) {
      $range = explode("-", $range);
      $start = strtotime($range[0]);
      $end = strtotime($range[1]);
      if($end >= $start) {
          $end = strtotime($range[1] . ' - 1 day');
      }
      if (($end >= $now)) {
          $is_open ++;
      }
  }

}

Ps : Just draft code need to be improved and factorised !!

gridnik commented 9 years ago

@coryetzkorn: Thank you for this code @braininteractive: Thank you for the fix. It worked.