2015-08-17 82 views
0

我想定義一組7天工作周的開放時間。當訪問者訪問網頁時,我想在特定國家(加勒比海尼維斯)顯示當前時間和日期,然後將其與一組定義的開放時間進行比較,以顯示兩個字幕中的一個1)打開2)關閉。具體來說,這是我想生產什麼:將當前時間與數組中的值進行比較true/false

enter image description here

我使用這個迄今爲止獲得當前的時間和設置的陣列,但我怎麼比較這兩個?

<?php 
    function get_timee($country,$city) { 
     $country = str_replace(' ', '', $country); 
     $city = str_replace(' ', '', $city); 
     $geocode_stats = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$city+$country,&sensor=false"); 
     $output_deals = json_decode($geocode_stats); 
     $latLng = $output_deals->results[0]->geometry->location; 
     $lat = $latLng->lat; 
     $lng = $latLng->lng; 
     $google_time = file_get_contents("https://maps.googleapis.com/maps/api/timezone/json?location=$lat,$lng&timestamp=1331161200&key=xxx"); 
     $timez = json_decode($google_time); 
     $d = new DateTime("now", new DateTimeZone($timez->timeZoneId)); 
     return $d->format('H:i'); 
    } 

    $array = array(
     "Monday" => "10:00 - 18:00", 
     "Tuesday" => "10:00 - 18:00", 
     "Wednesday" => "10:00 - 18:00", 
     "Thursday" => "10:00 - 18:00", 
     "Friday" => "18:00 - 23:00", 
     "Saturday" => "18:00 - 23:00", 
     "Sunday" => "Closed" 
    ); 

    ?> 

<?php echo get_timee("Federation of Saint Kitts and Nevis","Nevis"); ?>。我們目前...

+3

你不應該公開分享您的API密鑰。 – treegarden

+1

我把它刪除了,比尼維斯晚得多..我的意思是永遠不會。 – RiggsFolly

回答

1

你可以做這一切,而不必使用谷歌任何東西。我假設你是這樣做的,這樣你就可以在客戶的位置找出時間。或者,也許是因爲您不確定服務器上的時區設置爲什麼,或者您知道它設置爲您所在位置的錯誤時區。

但是,您可以使用這樣的核心PHP DateTime()DateTimeZone()這樣做。

注意:我更改了打開/關閉時間數組,以便於處理!

<?php 

    $opening_times = array(
     "Monday" => array('open' => '10:00', 'close' => '18:00'), 
     "Tuesday" => array('open' => '10:00', 'close' => '18:00'), 
     "Wednesday" => array('open' => '10:00', 'close' => '18:00'), 
     "Thursday" => array('open' => '10:00', 'close' => '18:00'), 
     "Friday" => array('open' => '18:00', 'close' => '23:00'), 
     "Saturday" => array('open' => '18:00', 'close' => '23:00'), 
     "Sunday" => "Closed" 
    ); 


function AreWeOpen($were_open, $date) 
{ 
    $htm = ''; 
    if (! is_array($were_open[$date->format('l')]) 
     && strtolower($were_open[$date->format('l')]) == 'closed') 
    { 
     $htm = 'We are closed all day Sunday'; 
    } else { 
     if ( $date->format('H:i') >= $were_open[$date->format('l')]['open'] 
      && $date->format('H:i') <= $were_open[$date->format('l')]['close'] 
      ) 
     { 
      $htm = 'We are open'; 
     } else { 
      $htm = 'We are closed'; 
     } 
    } 
    return $htm; 
} 

現在運行/測試上述所有你要做的是:

// set date time to NOW 
    $date = new DateTime(null, new DateTimeZone('America/St_Kitts')); 
    echo 'Are we open now? Now is ' . $date->format('l H:i') . ' >'; 
    echo AreWeOpen($opening_times, $date); 

    echo PHP_EOL; 

    echo 'Are we open at 09:59 Monday > '; 
    $date = new DateTime('2015/08/17 09:59:00', new DateTimeZone('America/St_Kitts')); 
    echo AreWeOpen($opening_times, $date); 

    echo PHP_EOL; 

    echo 'Are we open at 10:00 Monday > '; 
    $date = new DateTime('2015/08/17 10:00:00', new DateTimeZone('America/St_Kitts')); 
    echo AreWeOpen($opening_times, $date); 

    echo PHP_EOL; 

    echo 'Are we open at 18:00 Monday > '; 
    $date = new DateTime('2015/08/18 18:00:00', new DateTimeZone('America/St_Kitts')); 
    echo AreWeOpen($opening_times, $date); 

    echo PHP_EOL; 

    echo 'Are we open at 18:01 Monday > '; 
    $date = new DateTime('2015/08/18 18:01:00', new DateTimeZone('America/St_Kitts')); 
    echo AreWeOpen($opening_times, $date); 

    echo PHP_EOL; 

    echo 'Are we open at 18:01 Friday > '; 
    $date = new DateTime('2015/08/21 18:01:00', new DateTimeZone('America/St_Kitts')); 
    echo AreWeOpen($opening_times, $date); 


    echo PHP_EOL; 

    echo 'Are we open on SUNDAY > '; 
    $date = new DateTime('2015/08/16 18:01:00', new DateTimeZone('America/St_Kitts')); 
    echo AreWeOpen($opening_times, $date); 

而且,讓這些結果:

Are we open now? Now is Monday 07:38 >We are closed 
Are we open at 09:59 Monday > We are closed 
Are we open at 10:00 Monday > We are open 
Are we open at 18:00 Monday > We are open 
Are we open at 18:01 Monday > We are closed 
Are we open at 18:01 Friday > We are open 
Are we open on SUNDAY > We are closed all day Sunday 
+0

這太棒了。感謝您的解釋。是的,我使用谷歌來獲得尼維斯時代,但你的解決方案否定了所有這一切,甚至更好。 – egr103

1

一個解決方案的概要:

  1. 計算的數字版本的開放時間排列的。這個數組可以是一個星期幾到開放時間的映射,在DateTime-> Format('w')的同一時間索引日期,從星期日的0開始,等等。每個條目在沒有開放時間的情況下可以爲null,或包含商店打開日期的秒數的數組。

  2. 更改get_timee以返回DateTime對象iteself。

  3. 確定計算的DateTime對象是否落入當前日期的範圍內。特別是,使用(int)$ dateTime-> format('w')來獲取索引,然後在你的數組中查看。如果條目不爲空,則執行dateTime項目的「秒」計算,並確定它是否在範圍內。

相關問題