Day difference without weekends

2019-01-04 10:53发布

I want to count the total day difference from user input

For example when the user inputs

start_date = 2012-09-06 and end-date = 2012-09-11

For now I am using this code to find the diffeence

$count = abs(strtotime($start_date) - strtotime($end_date));
$day   = $count+86400;
$total = floor($day/(60*60*24));

The result of total will be 6. But the problem is that I dont want to include the days at weekend (Saturday and Sunday)

2012-09-06
2012-09-07
2012-09-08 Saturday
2012-09-09 Sunday
2012-09-10
2012-09-11

So the result will be 4

----update---

I have a table that contains date,the table name is holiday date

for example the table contains 2012-09-07

So, the total day will be 3, because it didn't count the holiday date

how do I do that to equate the date from input to date in table?

9条回答
我只想做你的唯一
2楼-- · 2019-01-04 11:28

Have a look at this post: Calculate business days

(In your case, you could leave out the 'holidays' part since you're after working/business days only)

<?php
//The function returns the no. of business days between two dates
function getWorkingDays($startDate,$endDate){
    // do strtotime calculations just once
    $endDate = strtotime($endDate);
    $startDate = strtotime($startDate);


    //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
    //We add one to inlude both dates in the interval.
    $days = ($endDate - $startDate) / 86400 + 1;

    $no_full_weeks = floor($days / 7);
    $no_remaining_days = fmod($days, 7);

    //It will return 1 if it's Monday,.. ,7 for Sunday
    $the_first_day_of_week = date("N", $startDate);
    $the_last_day_of_week = date("N", $endDate);

    //---->The two can be equal in leap years when february has 29 days, the equal sign is added here
    //In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
    if ($the_first_day_of_week <= $the_last_day_of_week) {
        if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
        if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
    }
    else {
        // (edit by Tokes to fix an edge case where the start day was a Sunday
        // and the end day was NOT a Saturday)

        // the day of the week for start is later than the day of the week for end
        if ($the_first_day_of_week == 7) {
            // if the start date is a Sunday, then we definitely subtract 1 day
            $no_remaining_days--;

            if ($the_last_day_of_week == 6) {
                // if the end date is a Saturday, then we subtract another day
                $no_remaining_days--;
            }
        }
        else {
            // the start date was a Saturday (or earlier), and the end date was (Mon..Fri)
            // so we skip an entire weekend and subtract 2 days
            $no_remaining_days -= 2;
        }
    }

    //The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
//---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
   $workingDays = $no_full_weeks * 5;
    if ($no_remaining_days > 0 )
    {
      $workingDays += $no_remaining_days;
    }    


    return $workingDays;
}

// This will return 4
echo getWorkingDays("2012-09-06","2012-09-11");
?>
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-04 11:32
/**
 * Getting the Weekdays count[ Excludes : Weekends]
 * 
 * @param type $fromDateTimestamp
 * @param type $toDateTimestamp
 * @return int
 */
public static function getWeekDaysCount($fromDateTimestamp = null, $toDateTimestamp=null) {

    $startDateString   = date('Y-m-d', $fromDateTimestamp);
    $timestampTomorrow = strtotime('+1 day', $toDateTimestamp);
    $endDateString     = date("Y-m-d", $timestampTomorrow);
    $objStartDate      = new \DateTime($startDateString);    //intialize start date
    $objEndDate        = new \DateTime($endDateString);    //initialize end date
    $interval          = new \DateInterval('P1D');    // set the interval as 1 day
    $dateRange         = new \DatePeriod($objStartDate, $interval, $objEndDate);

    $count = 0;

    foreach ($dateRange as $eachDate) {
        if (    $eachDate->format("w") != 6 
            &&  $eachDate->format("w") != 0 
        ) {
            ++$count;
        }
    }
    return $count;
}
查看更多
姐就是有狂的资本
4楼-- · 2019-01-04 11:37

use DateTime:

$datetime1 = new DateTime('2012-09-06');
$datetime2 = new DateTime('2012-09-11');
$interval = $datetime1->diff($datetime2);
$woweekends = 0;
for($i=0; $i<=$interval->d; $i++){
    $modif = $datetime1->modify('+1 day');
    $weekday = $datetime1->format('w');

    if($weekday != 0 && $weekday != 6){ // 0 for Sunday and 6 for Saturday
        $woweekends++;  
    }

}

echo $woweekends." days without weekend";

// 4 days without weekends
查看更多
登录 后发表回答