2015-07-09 47 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