PHP天差计算错误


PHP days difference calculation error

我有一些PHP代码来计算两个特定日期之间的天数。这个差额不包括星期天和星期六。此外,我还有一个日期数组,其中包括假日,也需要跳过。

起始日期为01-05-2015,结束日期为01-06-2015。我把五月的全部日子作为数组。因此差额应该是1天。但是我得到的输出是7。有什么问题吗?代码如下:

function dateRange($first, $last) {
    $dates = array();
    $current = strtotime($first);
    $now = $current;
    $last = strtotime($last);
    while( $current <= $last ) {
        if (date('w', $current) != 0){
            $dates[] = date('d-m-Y', $current);
        }
        $current = strtotime('+1 day', $current);
    }
    unset($dates[0]);
    return $dates;
}

$datea = "01-05-2015";
$date = "01-06-2015";
$hdsarray = array("1-05-2015","2-05-2015","4-05-2015","5-05-2015","7-05-2015","8-05-2015","9-05-2015","11-05-2015","12-05-2015","14-05-2015","15-05-2015","16-05-2015","18-05-2015","19-05-2015","21-05-2015","22-05-2015","23-05-2015","25-05-2015","26-05-2015","28-05-2015","29-05-2015","30-05-2015");
$datesarray = dateRange($datea, $date);
$result = array_diff($hdsarray,$datesarray);
$date_diff = sizeof($result);
echo $date_diff;

我能看到的唯一问题是在array_diff的使用中,它实际上包括了dateRange函数排除的卫星和太阳,如果没有在假期列表中找到。

相反,您可以在dateRange函数中传递您的假期日期,并在那里进行过滤。

function dateRange($first, $last, $excludeDates) {
    $dates = array();
    $current = strtotime($first);
    $now = $current;
    $last = strtotime($last);
    while( $current <= $last ) {
        if (date('w', $current) != 0 && date('w', $current) != 6 && !in_array(date('j-m-Y', $current), $excludeDates)){
            $dates[] = date('d-m-Y', $current);
        }
        $current = strtotime('+1 day', $current);
    }
    return $dates;
}
$datea = "01-05-2015";
$date = "01-06-2015";
$hdsarray = array("1-05-2015","2-05-2015","4-05-2015","5-05-2015","7-05-2015","8-05-2015","9-05-2015","11-05-2015","12-05-2015","14-05-2015","15-05-2015","16-05-2015","18-05-2015","19-05-2015","21-05-2015","22-05-2015","23-05-2015","25-05-2015","26-05-2015","28-05-2015","29-05-2015","30-05-2015");
$datesarray = dateRange($datea, $date, $hdsarray);print_r($datesarray);
结果:

Array
(
    [0] => 06-05-2015
    [1] => 13-05-2015
    [2] => 20-05-2015
    [3] => 27-05-2015
    [4] => 01-06-2015
)

所有的5个日期都出现在结果中,不是星期六,星期天,也不在假期列表中。

这里似乎有几个问题。首先,正如其他人指出的条件:

if (date('w', $current) != 0){

只检查星期日,如果它还应该包括星期六,它应该是:

if (date('w', $current) != 0 && date('w', $current) != 6){

其次,$hdsarray数组似乎不包含5月份的所有天数。似乎所有的星期三都不见了。

第三个问题是你在两个数组上使用array_diff,一个包含日期,另一个包含字符串。来自文档:

当且仅当(string) $elem1 ===两个元素相等(字符串)elem2美元。以字表示:当字符串表示相同时。

在您的$hdsarray中,您使用"1-05-2015"表示每月的第一天,而:

 echo date('d-m-Y', strtotime("1-05-2015"));

结果为"01-05-2015"。您需要在$hdsarray中为这些日期添加一个额外的0,或者也可以使用日期。

最后但并非最不重要的是,如果$hdsarray包含星期六或星期日的日期,当前的算法将不能正确工作,array_diff的结果仍然包含这些日期。因为您想要过滤daterange的结果,所以array_filter函数可能更合适。

尽管已经提供了答案,但这里有一个小片段,其中一个类为您处理一切:

<?php
class dateRange {
    protected $start, $end, $daysToExclude, $datesToExclude;
    function __construct($dateStart, $dateEnd, $daysToExclude, $datesToExclude) {
        $this->start            =   $dateStart;
        $this->end              =   $dateEnd;
        $this->daysToExclude    =   $daysToExclude;
        $this->datesToExclude   =   $this->fixFormat($datesToExclude);
    }
    public function getRangeLength ($callback = null) {
        $tmp    =   array();
        $now    =   strtotime($this->start);
        $to     =   strtotime($this->end);
        while ( $now <= $to ) {
            if (!in_array(date("w", $now), $this->daysToExclude)) {
                $tmp[] = date('d-m-Y', $now);
            }
            $now = strtotime('+1 day', $now);
        }
        is_callable($callback) && call_user_func($callback, array_diff($tmp,$this->datesToExclude));
        return count(array_diff($tmp,$this->datesToExclude));
    }
    private function fixFormat($el) {
        if (!is_array($el)) {
            return false;
        }
        else {
            foreach ($el as &$value) {
                $value  =   date("d-m-Y",strtotime($value));
            }
            return $el;
        }
    }
}
?>

我决定保留你现在的逻辑(使用date_diff),但我认为,在未来,你可能会有你的老板告诉你"你知道吗?所以,在目前的系统中,你将不得不手动编辑你的函数,也许,你将不再记得你做了什么。

上面的类需要四个形参:

  • dateStart (d-m-Y格式)
  • 日期结束(d-m-Y格式)
  • daysToExclude(包含要排除的天数的id的数组->用于排除星期六和星期日的示例数组(0,6))。
  • datesToExclude(包含要排除的日期的数组,支持的每种格式)。

该类将自动修复datesToExclude数组格式,以便允许您使用date_diff。

下面是一个使用它的例子:

<?php
    $dateStart      = "01-05-2015";
    $dateEnd        = "01-06-2015";
    $daysToExclude  = array(0,6); 
    $exclusions = array(
                "1-05-2015",
                "2-05-2015",
                "4-05-2015",
                "5-05-2015",
                "7-05-2015",
                "8-05-2015",
                "9-05-2015",
                "11-05-2015",
                "12-05-2015",
                "14-05-2015",
                "15-05-2015",
                "16-05-2015",
                "18-05-2015",
                "19-05-2015",
                "21-05-2015",
                "22-05-2015",
                "23-05-2015",
                "25-05-2015",
                "26-05-2015",
                "28-05-2015",
                "29-05-2015",
                "30-05-2015"
            );
            $dateRange = new dateRange($dateStart, $dateEnd, $daysToExclude, $exclusions);
            echo $dateRange->getRangeLength();
?>

上面的代码输出5.

函数getRangeLength也接受回调,并将返回date_diff操作的array结果,因此您还可以:

$dateRange->getRangeLength(function($res) {
    echo "Literal output: <br />";
    print_r($res);
    echo "<br />count is: "  . count($res);
});

以上输出:

Literal output: 
Array ( [3] => 06-05-2015 [8] => 13-05-2015 [13] => 20-05-2015 [18] => 27-05-2015 [21] => 01-06-2015 ) 
count is: 5

所以,如果你以后也需要删除星期一,你将能够很容易地做到这一点,通过更改daysToExclude为array(0,1,6);

希望这将有助于任何人谁将需要这个,尽管一个有效的答案已经张贴。

你原来的问题,在任何情况下,几乎与array_diff函数有关,它没有做它的工作,因为日期字符串不兼容,因为"1-01-2015"不同于"01-01-2015",除非你先把它们都转换成时间,然后再回到日期。

代码很好(除了根本没有使用$now)。问题是$hdsarray是错误的:

应该是$hdsarray = array("01-05-2015", "02-05-2015", "04-05-2015", "05-05-2015", "07-05-2015", "08-05-2015", "09-05-2015",...);

date('d-m-Y', $current);将始终返回1到9之间的所有天数的前导0。

这就是区别所在