计算两个日期时间(不包括周末)之间的时差

时间:2018-11-05 19:13:23

标签: php mysql mysqli

例如 02-11-2018 03:00 pm-05-11-2018 11:00 am应该是2小时。 因为3号和4号是周末。

$date1 = "2018-03-01 11:12:45"; 
$date2 = "2018-03-04 15:37:04";  
$date1Timestamp = strtotime($date1); 
$date2Timestamp = strtotime($date2); 
$difference = $date2Timestamp - $date1Timestamp; 
echo $difference; 

2 个答案:

答案 0 :(得分:2)

您可以使用mktime()为要比较的两个日期/时间创建UNIX时间戳。这些时间戳将表示Unix纪元(1970年1月1日00:00:00 GMT)与指定时间之间的秒数。由于它们都以秒为单位,因此可以很容易地计算出两个时间戳之间的秒数:

<?php

//set start time and end time - mktime(hour, minute, second, month, day, year)
$startTime = mktime(15, 0, 0, 11, 2, 2018); // 2-11-2018 3:00PM
$endTime = mktime(11, 0, 0, 11, 5, 2018); // 5-11-2018 11:00AM

//calculate total number of seconds between two date/times
$totalSeconds = $endTime - $startTime;

//apply whatever other math you need...

?>

就考虑到周末和营​​业时间而言,您将需要发挥创意,确定两个日期/时间之间有多少个周末,以及营业日的营业时间属于哪些时间。 date函数的PHP手册将派上用场。以下代码产生您想要的结果:

<?php
//set business start and end hours
$businessStartHour = 10; //10 AM
$businessEndHour = 16; //4 PM

//set weekend days
$arrWeekendDays = array(6,0); //numeric representations of Saturday (6) and Sunday (0)

//set start and end dates and times
//2-11-2018 3 PM
$startHour = 15;
$startMinute = 0;
$startSecond = 0;
$startMonth = 11;
$startDay = 2;
$startYear = 2018;

//5-11-2018 11 AM
$endHour = 11;
$endMinute = 0;
$endSecond = 0;
$endMonth = 11;
$endDay = 5;
$endYear = 2018;

//create UNIX timestamps
$startTime = mktime($startHour, $startMinute, $startSecond, $startMonth, $startDay, $startYear);
$endTime = mktime($endHour, $endMinute, $endSecond, $endMonth, $endDay, $endYear);

//ensure $endTime is greater than $startTime
if($startTime >= $endTime){
    //invalid start and end datetimes
    die("Invalid start and end datetimes.");
}

//calculate eligible seconds from partial time on first and last day
$totalSeconds = 0;

$currentTime = mktime(0, 0, 0, $startMonth, $startDay, $startYear); //beginning of $startTime day
$lastFullDay = mktime(0, 0, 0, $endMonth, $endDay, $endYear); //beginning of $endTime day

$startingBusinessTime = mktime($businessStartHour, 0, 0, $startMonth, $startDay, $startYear);
$endingBusinessTime = mktime($businessEndHour, 0, 0, $endMonth, $endDay, $endYear);

if($startTime < $startingBusinessTime){
    $startTime = $startingBusinessTime;
}
if($endTime > $endingBusinessTime){
    $endTime = $endingBusinessTime;
}

if($currentTime == $lastFullDay){
    //$startTime and $endTime occur on the same day
    if($endTime > $startTime){
        $totalSeconds += ($endTime - $startTime);
    }
}else{
    //$startTime and $endTime do not occur on the same day
    $startingBusinessTime = mktime($businessStartHour, 0, 0, $endMonth, $endDay, $endYear);
    $endingBusinessTime = mktime($businessEndHour, 0, 0, $startMonth, $startDay, $startYear);
    if($endingBusinessTime > $startTime){
        $totalSeconds += ($endingBusinessTime - $startTime);
    }
    if($endTime > $startingBusinessTime){
        $totalSeconds += ($endTime - $startingBusinessTime);
    }
}

//calculate eligible seconds from all full days in between start day and end day
$fullDayBusinessSeconds = (($businessEndHour - $businessStartHour) * 3600);

//set $currentTime to beginning of first full day
$nextDay = $currentTime + (26 * 3600); //add 26 hours to $currentTime to get into the next day, compensating for possible daylight savings
$currentTime = mktime(0, 0, 0, date('n', $nextDay), date('j', $nextDay), date('Y', $nextDay));

while($currentTime < $lastFullDay){
    //determine if $currentTime is a weekday
    if(!in_array(date('w', $currentTime), $arrWeekendDays)){
        //it's a business day, add all business seconds to $totalSeconds
        $totalSeconds += $fullDayBusinessSeconds;
    }
    //increment $currentTime to beginning of next day
    $nextDay = $currentTime + (26 * 3600); //add 26 hours to $currentTime to get into the next day, compensating for possible daylight savings
    $currentTime = mktime(0, 0, 0, date('n', $nextDay), date('j', $nextDay), date('Y', $nextDay));
}

echo "Total eligible time between start time and end time: " . $totalSeconds . " seconds (" . convertSecToTime($totalSeconds) . ")";

function convertSecToTime($sec) 
 {
  $date1 = new DateTime("@0");
  $date2 = new DateTime("@$sec");
  $interval =  date_diff($date1, $date2);
  return $interval->format('%y Years, %m months, %d days, %h hours, %i minutes and %s seconds');
  // convert into Days, Hours, Minutes
  // return $interval->format('%a days, %h hours, %i minutes and %s seconds'); 
  }
?>

答案 1 :(得分:-1)

请仔细看看这个精确的php函数,其中不包括周末,返回天数。

$start= "2018-03-01 11:12:45"; 
$end= "2018-04-01 15:37:04";

echo Count_Days_Without_Weekends($start, $end);

function Count_Days_Without_Weekends($start, $end){
    $days_diff = floor(((abs(strtotime($end) - strtotime($start))) / (60*60*24)));
    $run_days=0;
    for($i=0; $i<=$days_diff; $i++){
        $newdays = $i-$days_diff;
        $futuredate = strtotime("$newdays days");
        $mydate = date("F d, Y", $futuredate);
        $today = date("D", strtotime($mydate));             
        if(($today != "Sat") && ($today != "Sun")){
            $run_days++;
        }
    }
return $run_days;
}

尝试一下,它确实有效。

相关问题