2013-02-15 87 views
12

如果時間是AM到PM(例如:上午11點 - 下午10點),但是如果地點的營業時間是AM到AM(例如:9 AM - 1 AM)它打破了。這裏是我的代碼:根據營業時間確定業務是否開啓/關閉

$datedivide = explode(" - ", $day['hours']); //$day['hours'] Example 11:00 AM - 10:00 PM 
$from = ''.$day['days'].' '.$datedivide[0].''; 
$to = ''.$day['days'].' '.$datedivide[1].''; 
$date = date('l g:i A'); 
$date = is_int($date) ? $date : strtotime($date); 
$from = is_int($from) ? $from : strtotime($from); 
$to = is_int($to) ? $to : strtotime($to); 
if (($date > $from) && ($date < $to) && ($dateh != 'Closed')) { 
    ?> 
    <script type="text/javascript"> 
    $(document).ready(function(){ 
     $('.entry-title-container').append('<div class="column two"><h2 style="color:green;text-align: left;margin: 0;">OPEN<br /><span style="color:#222;font-size:12px;display: block;">Closes at <?php echo $datedivide[1]; ?></span></h2></div><br clear="all" />'); 
    }); 
    </script> 
    <?php 
} 
+0

我要去玩這個,謝謝:) – Stephen 2013-02-16 00:31:21

+0

你每天的工作時間和工作時間是什麼? – 2013-02-16 01:17:42

+0

週一:9:00 AM - 12:00 AM 週二:9:00 AM - 12:00 AM 週三:9:00 AM - 12:00 AM 週四:9:00 AM - 12:00 AM 星期五:上午9:00 - 上午1:00 星期六:上午9:00 - 上午1:00 星期日:上午9:00 - 上午12:00 – Stephen 2013-02-16 23:54:55

回答

30

您首先需要創建一個數組來保存一週中的日子以及它們各自的關閉/打開時間範圍。

/** 
* I setup the hours for each day if they carry-over) 
* everyday is open from 09:00 AM - 12:00 AM 
* Sun/Sat open extra from 12:00 AM - 01:00 AM 
*/ 
$storeSchedule = [ 
    'Sun' => ['12:00 AM' => '01:00 AM', '09:00 AM' => '12:00 AM'], 
    'Mon' => ['09:00 AM' => '12:00 AM'], 
    'Tue' => ['09:00 AM' => '12:00 AM'], 
    'Wed' => ['09:00 AM' => '12:00 AM'], 
    'Thu' => ['09:00 AM' => '12:00 AM'], 
    'Fri' => ['09:00 AM' => '12:00 AM'], 
    'Sat' => ['12:00 AM' => '01:00 AM', '09:00 AM' => '12:00 AM'] 
]; 

然後循環顯示當天的時間範圍並檢查當前時間或提供的時間戳是否在一個範圍內。您可以通過使用DateTime類爲每個時間範圍的開始/結束時間生成一個對象。

下面將做到這一點,並允許您指定一個時間戳,以防您想檢查提供的時間戳而不是當前時間。

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

// default status 
$status = 'closed'; 

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

// loop through time ranges for current day 
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) { 

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

    // check if current time is within a range 
    if (($startTime < $currentTime) && ($currentTime < $endTime)) { 
     $status = 'open'; 
     break; 
    } 
} 

echo "We are currently: $status"; 

參照上述

+1

看起來不錯:)謝謝! – Stephen 2013-02-18 03:24:13

+0

+不錯,但如果你使用'DateTime',我會首選的 – Baba 2013-02-26 11:39:19

+2

上面的例子是完美的......做得好:) – Baba 2013-02-26 12:42:01

0

我會建議使用PHP的DateTime類。它的開發是爲了解決所有這些問題,通過提供一個簡單的界面來完成像這樣的任務。

+1

你能舉個例子嗎我如何將它與我的腳本整合? – Stephen 2013-02-16 00:31:03

2

你應該重新集結所有開放時間一數組中,因爲開口小時昨天,如果你留在午夜後打開可能是影響DEMO。也有可能每天有幾個開放時間可能會很方便。

<?php 

$times = array(
    'mon' => '9:00 AM - 12:00 AM', 
    'tue' => '9:00 AM - 12:00 AM', 
    'wed' => '9:00 AM - 12:00 AM', 
    'thu' => '9:00 AM - 12:00 AM', 
    'fri' => '9:00 AM - 1:00 AM', 
    'sat' => '9:00 AM - 1:00 PM, 2:00 PM - 1:00 AM', 
    'sun' => 'closed' 
); 

function compileHours($times, $timestamp) { 
    $times = $times[strtolower(date('D',$timestamp))]; 
    if(!strpos($times, '-')) return array(); 
    $hours = explode(",", $times); 
    $hours = array_map('explode', array_pad(array(),count($hours),'-'), $hours); 
    $hours = array_map('array_map', array_pad(array(),count($hours),'strtotime'), $hours, array_pad(array(),count($hours),array_pad(array(),2,$timestamp))); 
    end($hours); 
    if ($hours[key($hours)][0] > $hours[key($hours)][1]) $hours[key($hours)][1] = strtotime('+1 day', $hours[key($hours)][1]); 
    return $hours; 
} 

function isOpen($now, $times) { 
    $open = 0; // time until closing in seconds or 0 if closed 
    // merge opening hours of today and the day before 
    $hours = array_merge(compileHours($times, strtotime('yesterday',$now)),compileHours($times, $now)); 

    foreach ($hours as $h) { 
     if ($now >= $h[0] and $now < $h[1]) { 
      $open = $h[1] - $now; 
      return $open; 
     } 
    } 
    return $open; 
} 

$now = strtotime('7:59pm'); 
$open = isOpen($now, $times); 

if ($open == 0) { 
    echo "Is closed"; 
} else { 
    echo "Is open. Will close in ".ceil($open/60)." minutes"; 
} 

?> 

。如果您只是想解決與像9am - 5am時間的問題。另一方面,你應該檢查是否$from > $to,如果需要1添加一天$to

0

也許這樣?我發現它here並調整了一下。

<?php 
    date_default_timezone_set('Europe/Stockholm'); // timezone 
    $today = date(N); // today ex. 6 

    //Opening hours 
    $opening_hours[1] = "12:00"; $closing_hours[1] = "00:00"; 
    $opening_hours[2] = "12:00"; $closing_hours[2] = "00:00"; 
    $opening_hours[3] = "12:00"; $closing_hours[3] = "00:00"; 
    $opening_hours[4] = "12:00"; $closing_hours[4] = "02:00"; 
    $opening_hours[5] = "12:00"; $closing_hours[5] = "04:00"; 
    $opening_hours[6] = "13:00"; $closing_hours[6] = "04:00"; 
    $opening_hours[7] = "13:00"; $closing_hours[7] = "00:00"; 

    // correction for opening hours after midnight. 
    if(intval($closing_hours[$today - 1]) > 0) 
     $today = date(N,strtotime("-".intval($closing_hours[$today - 1])." hours")); 

    // now check if the current time is after openingstime AND smaller than closing hours of tomorrow 
      if (

    (date("H:i") > $opening_hours[$today] && 
    ((date("Y-m-d H:i") < date("Y-m-d H:i", strtotime('tomorrow +'.intval($closing_hours[$today]).' hours')))) 
    ) || 
    date("H:i") < $closing_hours[$today] && 
    ((date("Y-m-d H:i") < date("Y-m-d H:i", strtotime('today +'.intval($opening_hours[$today]).' hours')))) 

    ) { 
     $label = "label-success"; 
     $label_text = "OPEN"; 
     $label_time = $closing_hours[$today]; 
    } else { 
     $label = "label-danger"; 
     $label_text = "GESLOTEN"; 
     $label_time = $opening_hours[$today]; 
    } 
    ?> 
0

我試過這段代碼幾天不同的方式,發現這對我來說是一個解決方案。發佈它可以幫助其他人。將AM/PM格式的時間轉換回UNIX時間非常重要。

我使用從數據庫中提取的時間,所以你必須定義$startTime$endTime

// define each days time like so 
$monsta = "07:00 AM"; 
$monend = "05:00 PM"; 

$storeSchedule = [ 
    'Mon' => ["$monsta" => "$monend"], 
    'Tue' => ["$tuessta" => "$tuesend"], 
    'Wed' => ["$wedssta" => "$wedsend"], 
    'Thu' => ["$thurssta" => "$thursend"], 
    'Fri' => ["$frista" => "$friend"], 
    'Sat' => ["$satsta" => "$satend"], 
    'Sun' => ["$sunsta" => "$sunend"] 
]; 

// default time zone 
$timestamp = time() - 18000; 

// default status 
$status = 'Closed'; 

// loop through time ranges for current day 
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) { 

// create time objects from start/end times 
$startTime = strtotime($startTime); 
$endTime = strtotime($endTime); 

// check if current time is within a range 
if ($startTime <= $timestamp && $timestamp <= $endTime) { 
    $status = 'Open'; 

    } 
} 

echo '<p><b>Today\'s Hours</b><br />'.$days[$d].'<br />'; 
echo "<b>We are currently: $status </b></p>"; 
+0

照顧細緻? – RamenChef 2016-11-29 20:05:12

+0

首先在數組中定義$ startTime和$ endTime – 2016-11-29 20:08:08

+0

$ monsta =「07:00 AM」 – 2016-11-29 20:08:54

2

從一個AWS Debian的服務器(位於西海岸),其中我們的商店營業時間其實都是美國東部時間使用公認的答案修改......也投進一個PHP函數。

/* 
* decide based upon current EST if the store is open 
* 
* @return bool 
*/ 
function storeIsOpen() { 
    $status = FALSE; 
    $storeSchedule = [ 
     'Mon' => ['08:00 AM' => '05:00 PM'], 
     'Tue' => ['08:00 AM' => '05:00 PM'], 
     'Wed' => ['08:00 AM' => '05:00 PM'], 
     'Thu' => ['08:00 AM' => '05:00 PM'], 
     'Fri' => ['08:00 AM' => '05:00 PM'] 
    ]; 

    //get current East Coast US time 
    $timeObject = new DateTime('America/New_York'); 
    $timestamp = $timeObject->getTimeStamp(); 
    $currentTime = $timeObject->setTimestamp($timestamp)->format('H:i A'); 

    // loop through time ranges for current day 
    foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) { 

     // create time objects from start/end times and format as string (24hr AM/PM) 
     $startTime = DateTime::createFromFormat('h:i A', $startTime)->format('H:i A'); 
     $endTime = DateTime::createFromFormat('h:i A', $endTime)->format('H:i A'); 

     // check if current time is within the range 
     if (($startTime < $currentTime) && ($currentTime < $endTime)) { 
      $status = TRUE; 
      break; 
     } 
    } 
    return $status; 
} 
0

有點晚了,但我有一個解決方案,如果這裏的人不太適合他們的需求。我確實喜歡在午夜後關閉多天時間的解決方案,但這樣會增加額外的數據處理以顯示小時數。您首先必須檢查是否有多個可用時間集,然後確認他們已連接(兩者之間沒有時間)。

我的解決辦法,而不是寫你通過開啓時間的函數,關閉時間,時間在個問題,它會開放和假返回true封閉式:

function is_open($open, $close, $query_time){ 
    $open = new DateTime($open); 
    $close = new DateTime($close); 
    $query_time = new DateTime($query_time); 
    $is_open = false; 
    //Determine if open time is before close time in a 24 hour period 
    if($open < $close){ 
     //If so, if the query time is between open and closed, it is open 
     if($query_time > $open){ 
      if($query_time < $close){ 
       $is_open = true; 
      } 
     } 
    } 
    elseif($open > $close){ 
     $is_open = true; 
     //If not, if the query time is between close and open, it is closed 
     if($query_time < $open){ 
      if($query_time > $close){ 
       $is_open = false; 
      } 
     } 
    } 
    return $is_open; 
}