没有周末的日差

Bel*_*jar 24 php codeigniter date

我想计算用户输入的总日差

例如,当用户输入时

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

现在我正在使用此代码来查找差异

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

总计的结果将是6.但问题是我不想包括周末(周六和周日)的日子

2012-09-06
2012-09-07
2012-09-08 Saturday
2012-09-09 Sunday
2012-09-10
2012-09-11
Run Code Online (Sandbox Code Playgroud)

结果将是4

----更新---

我有一个包含日期的表,表名是假日日期

例如,表包含 2012-09-07

因此,总日数将是3,因为它没有计算假日日期

我该怎么做才能将日期与输入日期等同于表格?

Dan*_*Lee 47

很容易与我的最爱DateTime,DateIntervalDatePeriod

$start = new DateTime('2012-09-06');
$end = new DateTime('2012-09-11');
// otherwise the  end date is excluded (bug?)
$end->modify('+1 day');

$interval = $end->diff($start);

// total days
$days = $interval->days;

// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);

// best stored as array, so you can add more than one
$holidays = array('2012-09-07');

foreach($period as $dt) {
    $curr = $dt->format('D');

    // substract if Saturday or Sunday
    if ($curr == 'Sat' || $curr == 'Sun') {
        $days--;
    }

    // (optional) for the updated question
    elseif (in_array($dt->format('Y-m-d'), $holidays)) {
        $days--;
    }
}


echo $days; // 4
Run Code Online (Sandbox Code Playgroud)

  • 我相信在这段代码中,如果假期是星期六或星期日,那么它将被扣除两次.应该有一个"elseif". (8认同)

Lan*_*der 11

在我的情况下,我需要与OP相同的答案,但想要更小的东西.@ Bojan的答案有效,但我不喜欢它不能用于DateTime对象,需要使用时间戳,并且正在进行比较strings而不是实际的对象本身(感觉很讨厌)...这是他的答案的修订版本.

function getWeekdayDifference(\DateTime $startDate, \DateTime $endDate)
{
    $days = 0;

    while($startDate->diff($endDate)->days > 0) {
        $days += $startDate->format('N') < 6 ? 1 : 0;
        $startDate = $startDate->add(new \DateInterval("P1D"));
    }

    return $days;
}
Run Code Online (Sandbox Code Playgroud)

Per @ xzdead的评论如果您希望包含开始日期结束日期:

function getWeekdayDifference(\DateTime $startDate, \DateTime $endDate)
{
    $isWeekday = function (\DateTime $date) {
        return $date->format('N') < 6;
    };

    $days = $isWeekday($endDate) ? 1 : 0;

    while($startDate->diff($endDate)->days > 0) {
        $days += $isWeekday($startDate) ? 1 : 0;
        $startDate = $startDate->add(new \DateInterval("P1D"));
    }

    return $days;
}
Run Code Online (Sandbox Code Playgroud)


Mih*_*rga 8

用途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++){
    $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
Run Code Online (Sandbox Code Playgroud)


小智 6

在没有周末的情况下获得差异的最简单,最快捷的方法是使用Carbon库.

以下是如何使用它的示例:

<?php

$from = Carbon\Carbon::parse('2016-05-21 22:00:00');
$to = Carbon\Carbon::parse('2016-05-21 22:00:00');
echo $to->diffInWeekdays($from);
Run Code Online (Sandbox Code Playgroud)


Boj*_*vić 5

日期('N')获取星期几(1 - 星期一,7 - 星期日)

$start = strtotime('2012-08-06');
$end = strtotime('2012-09-06');

$count = 0;

while(date('Y-m-d', $start) < date('Y-m-d', $end)){
  $count += date('N', $start) < 6 ? 1 : 0;
  $start = strtotime("+1 day", $start);
}

echo $count;
Run Code Online (Sandbox Code Playgroud)