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

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

问题描述

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

I want to count the total day difference from user input

例如,当用户输入

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

现在我使用这个代码来查找diffeence

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));

总计的结果将是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

----更新---

----update---

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

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?

推荐答案

收藏夹: DateTime DateInterval DatePeriod

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天全站免登陆