我有一個函數返回兩個日期之間的差別,但是我需要計算出的差異在工作時間,假設週一至週五(上午9:00至下午5:30):calc下2日期間的工作時間在PHP
//DATE DIFF FUNCTION
// Set timezone
date_default_timezone_set("GMT");
// Time format is UNIX timestamp or
// PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 6) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array('year','month','day','hour','minute','second');
$diffs = array();
// Loop thru all intervals
foreach ($intervals as $interval) {
// Set default diff to 0
$diffs[$interval] = 0;
// Create temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
$time1 = $ttime;
$diffs[$interval]++;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
}
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
// Return string with times
return implode(", ", $times);
}
日期1 = 2012-03-24 3時58分58秒
日期2 = 2012-03-22 11點29分16秒
是否有這樣做的一個簡單的方法,即 - 計算一週內工作時間的百分比並將差值除以我們荷蘭國際集團上述功能 - 我打周圍的這個想法,得到了一些很奇怪的數字...
還是有更好的辦法....?
你不能做一週的比例:如果你有一個完整的星期六和星期日,那就是工作周的0%,大概是一週的29%。 要做到這一點的最快方法是找出工作的全天數,然後計算這些全天工作前後的部分天數。 – Jerome 2012-07-09 14:46:54