没有周末的日差 [英] Day difference without weekends

查看:38
本文介绍了没有周末的日差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to count the total day difference from user input

例如当用户输入时

start_date = 2012-09-06end-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));

total 的结果将是 6.但问题是我不想包括周末(周六和周日)的天数

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

所以结果是 4

----更新---

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

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

例如该表包含2012-09-07

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

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?

推荐答案

我最喜欢的很容易:DateTimeDateIntervalDatePeriod

Very easy with my favourites: DateTime, DateInterval and DatePeriod

$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

这篇关于没有周末的日差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆