2014-02-26 35 views
1

我有一個系統,檢查當前時間是否在香港工作時間。 我確定工作時間是從上午9點到下午6點。 我的代碼正在工作,但我覺得有一個更好/更漂亮的方式來編寫IF條件。檢查工作時間的時間 - 改善代碼/邏輯

噹噹前時間不在HK工作時間時,該函數將返回True。 它也會在一天結束之前的15分鐘(6點)返回TRUE(香港下班時間),並且在開放時間前30分鐘返回FALSE(作爲香港工作時間)。

function hkOffHours($endHour = 18, $startHour = 9) { 
    $offHour = FALSE; 

    // create the DateTimeZone object for later 
    $dtzone = new DateTimeZone('Asia/Hong_Kong'); 

    // first convert the timestamp into a string representing the local time 
    $time = date('r', time()); 

    // now create the DateTime object for this time 
    $dtime = new DateTime($time); 

    // convert this to the user's timezone using the DateTimeZone object 
    $dtime->setTimeZone($dtzone); 

    // print the time using your preferred format 
    $time = $dtime->format('g:i A m/d/y'); 

    $hour = $dtime->format('G'); 
    $min = $dtime->format('i'); 
    $day = $dtime->format('D'); 

    // if weekend 
    if(($day == 'Sun' || $day == 'Sat')) 
    { 
     $offHour = TRUE; 
    } 

    // if out of office hours 
    if (
     ($admin_group == '1') && 
     (
      ($hour == ($endHour-1) && $min>=45) || 
      ($hour >= $endHour) || 
      ($hour == ($startHour-1) && $min <= 30) || 
      ($hour <= ($startHour -2)) 
     ) 
    ) 
    { 
     $offHour = TRUE; 
    } 

    return $offHour;   
} 

我會感激你的想法。

+1

你可能更喜歡這個地方:http://codereview.stackexchange.com/ – Preexo

+0

謝謝,不熟悉 – Lupin

回答

0

那麼,我的第一個想法是,如果你還沒有這樣做,你可以通過將它分解成幾個方法和一個類來重構它。

例如:

$offHour = FALSE; 

    // create the DateTimeZone object for later 
    $dtzone = new DateTimeZone('Asia/Hong_Kong'); 

    // first convert the timestamp into a string representing the local time 
    $time = date('r', time()); 

    // now create the DateTime object for this time 
    $dtime = new DateTime($time); 

    // convert this to the user's timezone using the DateTimeZone object 
    $dtime->setTimeZone($dtzone); 

    // print the time using your preferred format 
    $time = $dtime->format('g:i A m/d/y'); 

    $hour = $dtime->format('G'); 
    $min = $dtime->format('i'); 
    $day = $dtime->format('D'); 

所有上述領域可能是類變量。

if(($day == 'Sun' || $day == 'Sat')) 

這可能被移動到不同的功能,稱爲isWeekend()

if (
    ($admin_group == '1') && 
    (
     ($hour == ($endHour-1) && $min>=45) || 
     ($hour >= $endHour) || 
     ($hour == ($startHour-1) && $min <= 30) || 
     ($hour <= ($startHour -2)) 
    ) 
) 
{ 
    $offHour = TRUE; 
} 

這可移至isOutOfOfficeHours()

全班模板:

class workingHours{ 

    private $dtzone; 
    private ... 
    (...) 

    public function __construct($dtzone....){ 
    //setup values of class members 
    } 

    private function isWeekend(){} //see above 

    private function isOutOfOfficeHours(){} //also see above 

    public function hkOffHours(){ 
    return $this->isWeekend() || $this->isOutOfOfficeHours(); 
    } 
} 

當然通知書,我寫了這快,我在工作。因此,我的代碼邏輯可能不是100%兼容你的。無論如何,我希望你能明白這個主意。

還有一個最後的想法 - 有許多關於重構的書籍。嘗試一個!他們會讓你成爲更好的編碼器。

+0

謝謝,我沒有重構的問題,但在我看來有點矯枉過正建立一個班級或這個簡單的任務,但會考慮它。謝謝 – Lupin

+0

起初聽起來有點過度,但相信我,事實並非如此。我不知道你是否與更大的項目有關。我做了,發現這基本上是一種很好的做法。有時候你很確定「我只會在這裏使用這種方法」。時間流逝,最終你會得到十幾份這樣或類似的方法。另一件事是通過分解和轉換爲類,您可以隨時重用其中的一些組件,例如實施戰略模式(http://bit.ly/1ftdIiM)以獲得其他國家的工作時間。這種方法也遵循DRY規則。 – ex3v

+0

謝謝@ ex3v,基本上我同意你的觀點,也許我太懶惰或者太重了,不能把它作爲一個類來完成,但儘管有一些邏輯把它作爲一個類來構建,但我仍然認爲有些地方是舊時尚功能足夠好 - 不要說對於更大的事情我不會使用OOP。無論如何,對IF聲明有任何想法,都會喜歡聽你的想法...... – Lupin