2013-02-19 36 views
0

我正在嘗試創建一個函數來計算2次之間的時間差,但只包括工作時間。 這是一個追蹤工作完成所需時間的項目。PHP - 僅使用工作時間的日期/時間差

工作時間爲週一至週五上午9點至下午5點。

  • 例1
    如果工作開始於下午4:50,有人第二天會根據網頁上午10時,計時器會說1H10米0。
  • 例子2
    如果工作從下午6點23分開始,有人在第二天上午9點30分看到頁面,計時器會說30米0秒。
  • 示例3
    如果工作從週五下午1:20開始,有人在週四下午4點看到頁面,則計時器會說18h 40m 0s。 (它排除星期六和星期天!)
+0

查看本頁面的回答 [鏈接](http:// stackoverflow。com/questions/2091838 /時間差之間的2日期忽略週末時間) – 2013-02-19 13:47:42

+0

@彼得我試圖循環通過for循環的每個小時,並試圖縮小它與if語句,但它只是當我進入分鐘和秒鐘時,會變得混亂和混亂。如果你能告訴我你將如何去做,我現在真的有一個精神障礙,我會非常感激。 – Clarkey 2013-02-19 13:54:50

+0

@AmineMatmati我看了一下,這更側重於跳過週末而不是工作時間以外的所有事情。 – Clarkey 2013-02-19 14:00:05

回答

1

這可能有點壓倒性,但在鏈接@Amine Matmati提供了你所需要的所有工具。有了它,您可以輕鬆地從開始時間戳到現在,每天都可以遍歷,並且可以跳過週末和休息時間。

這有點笨重,但它會工作。

strtotime(str, timestamp)需要一個字符串,它是一個幾乎簡單的英語命令,如果你想要,一個參考時間戳。這是您將用於從當前時間戳一直運行到結束時間戳的內容。

即。 strtotime('end of the working day', timestamp of when the job started);

[注:「一天的結束」是不是一個可以接受的字符串,檢查鏈接到PHP手冊在底部,我只是想讓你知道它是如何對你有用]

所以,如果你建立了一個從原始時間標記走到現在的循環,你可以計算你工作時間的數量。

我會告訴你在僞代碼的邏輯:在工作時間

$Present is present timestamp; 
$Start is the starting timestamp; 
$Hours will be the accumulated working hours between starting timestamp and present timestamp; 

loop while present timestamp not reached { 

if $Start does not occur on the same day as $Present{ 
    if $Start occurs inside of working hours { 
    find the time to the end of the working day; 
    add that time to $Hours; 
    } 

    find next day; 
    if the next day is Saturday{ 
    update $Start to the working start of next Monday; 
    } 
    else{ 
    update $Start to the working start of the next day; 
    } 
} 
else{ 
    find the time between $Start and $Present; 
    add that time to $Hours; 
    update $Start to $Present or Break; 
    } 
} 

它(即前)假設起始時間不會在週末發生,並且不會發生外當前時間戳的同一天,但控制這些是非常容易的。

東西,這將成爲你的朋友在這個有:

@Amine Matmati的鏈接:Time difference between 2 dates ignoring weekend time

PHP日期():http://php.net/manual/en/function.date.php
PHP的strtotime():http://www.php.net/manual/en/function.strtotime.php

編碼愉快!