2015-07-09 91 views
-1

我想在我的网站上列出一些文字,说明一家商店是否根据其开放时间开放。如果商店是开放的,它说什么时候它是开放的。如果它没有打开,它会在下一次打开时显示。用PHP确定店铺开放时间

我已经存储在以下变量的开放时间:

$opening_times = [ 
    'Monday' => ['09:00' => '17:00'], 
    'Tuesday' => ['09:00' => '17:00'], 
    'Wednesday' => ['09:00' => '12:00'], 
    'Thursday' => ['09:00' => '17:00'], 
    'Friday' => ['09:00' => '17:00'], 
    'Saturday' => ['09:30' => '17:00'] 
]; 

商店周日不办公。

请有人指导我如何做到这一点?我已经看过this example,但我不确定如何处理显示下一次店铺开放的时间,以及当它是星期天时该怎么办。

我希望能完成一些展示下次店铺开放的事情,无论是否在同一天。例如,星期六下午5点30分,我希望消息说周一上午9点​​该商店的下一个营业时间。

我以前曾试图通过在$opening_times变量中存储下一个打开的日期和时间,但我想知道是否有更优雅的解决方案。

+0

每一天,店里两个值 - 'openTime'和'closeTime'你可以叫'日期()'来获得当前的小时和分钟;检查如果这些值在'openTime'之后,并且在今天的'closeTime'之前,那么对于星期天,您可以明确地检查今天是星期天,还是将'openTime'设置为'closeTime'之后,以便它永远不会显示为打开 – andrewsi

+0

你想要什么答案,如果说周四开放时间在星期五的02:00结束? –

+0

@RyanVincent简而言之,它永远不会;这个特定的商店永远不会在午夜开放,但为了争辩,在这种情况下,假设星期五上午9点仍然开放,我希望它在凌晨2点之后说“关闭到今天上午9点”,并且“今天开放至凌晨2点”b eforehand。 – DomStapleton

回答

2

使用这里的答案:Determine If Business Is Open/Closed Based On Business Hours作为指导。

这考虑到以后开放,而不是今天开放。

更新:告诉你它何时下一个打开。 (未经测试,因为工作的服务器使用PHP 5.3 :()

<?php 
$storeSchedule = [ 
    'Sun' => [['12:00' => '01:00', '09:00' => '12:00']], 
    'Mon' => [['09:00' => '12:00']], 
    'Tue' => [['09:00' => '12:00']], 
    'Wed' => [['09:00' => '12:00']], 
    'Thu' => [['09:00' => '12:00'], ['22:50' => '23:00']], 
    'Fri' => [['09:00' => '12:00']], 
    'Sat' => [['12:00' => '01:00', '09:00' => '12:00']] 
]; 



// current or user supplied UNIX timestamp 
$timestamp = time(); 

// default status 
$open = false; 

// Open later at 
$openAt = false; 

// get current time object 
$currentTime = (new DateTime())->setTimestamp($timestamp); 

// Current day 
$currentDay = date('D', $timestamp); 

if(isset($storeSchedule[$currentDay])){ 

    // loop through time ranges for current day 
    foreach ($storeSchedule[$currentDay] as $key => $dateGroup) { 
     $startTime = current(array_keys($dateGroup)); 
     $endTime = current(array_values($dateGroup)); 


     // create time objects from start/end times 
     $startTime = DateTime::createFromFormat('H:i', $startTime); 
     $endTime = DateTime::createFromFormat('H:i', $endTime); 

     // check if current time is within a range 
     if (($startTime < $currentTime) && ($currentTime < $endTime)) { 
      $open = true; 
      break; 
     }elseif($currentTime < $startTime){ 
      // Opening Later 
      $openAt = $startTime; 
     } 
    } 
}else{ 
    // Not open because day is not in array 

} 

if($open){ 
    echo "We are open"; 
}else{ 
    if($openAt){ 
     echo "We open later at " . $openAt->format('H:i'); 
    }else{ 
     // Get next open 
     $arrayDays = array_keys($storeSchedule);   // Get an array of the days 
     $arrayTimes = array_values($storeSchedule);   // Get an array of times 
     $dayIndex = array_search($currentDay, $arrayDays); // Find out what day we are in in the array. To see if there are more this week 
     $nextDay = ($dayIndex + 1) >= count($arrayDays) ? $arrayTimes[0] : $arrayTimes[$dayIndex + 1]; // If there are no more this week, take the first day, else take the next day. 
     $nextOpenTime = current(array_keys($nextDay)); // Take the first set of times from this day as the start time 
     $nextOpenDay = $arrayDays[$dayIndex + 1];  // Get the day key name 


     echo "We are not open"; 
     echo "We open next on " . $nextOpenDay . " at " . $nextOpenTime->format('H:i'); 
    } 
} 


?> 
+0

谢谢,@Christian。然而,我希望能够完成下一次店铺开放时展示的东西,无论是否在同一天。例如,星期六下午5点30分,我希望消息说周一上午9点​​该商店的下一个营业时间。 我以前曾试图通过在'$ opening_times'变量中存储下一个打开的日期和时间来解决这个问题,但我想知道是否有更优雅的解决方案。 – DomStapleton