PHP计算两个日期之间的天数

PHP计算两个日期之间的天数,php,date,Php,Date,我正在开发一个围绕日期的web应用程序 例如,我需要根据弹性天数计算数字-伪代码 $count_only = array('monday', 'wednesday', 'friday'); //count only these days $start_date = 1298572294; // a day in the past $finish_date = 1314210695; //another day $var = number_of_days_between($start_date

我正在开发一个围绕日期的web应用程序

例如,我需要根据弹性天数计算数字-伪代码

$count_only = array('monday', 'wednesday', 'friday'); //count only these days
$start_date = 1298572294;  // a day in the past
$finish_date = 1314210695; //another day

$var = number_of_days_between($start_date, $finish_date, $count_only);

是否有一种方法可以确定经过了多少完整的天数,而只计算某些天数?

当然有一种方法:-)

已经过去的日子只是简单的

$elapsed_days = floor(($finish_date-$start_date) / 86400);
这不会得到您需要的结果。您可以执行以下(pesudo)代码:

$eassed\u days=楼层($finish\u date-$start\u date)/86400);

对于(int$i=0;$i,您可以在
$count\u only
数组中创建一个循环,从
$start\u date
$end\u date
后停止(从函数返回)

function number_of_days_between($start_date, $finish_date, $count_only) {
    $count  = 0;
    $start  = new DateTime("@$start_date");
    $end    = new DateTime("@$finish_date");
    $days   = new InfiniteIterator(new ArrayIterator($count_only));
    foreach ($days as $day) {
        $count++;
        $start->modify("next $day");
        if ($start > $end) {
            return $count;
        }
    }
}

您可以通过计算两个指定日期之间的完整周数,然后计算开始/结束部分周数,以计算悬空日期,从而大大简化此过程

e、 g

然后,只需检查悬挂的日期:

Tuesday -> add 2 days for Wednesday+Friday to get to the end of the week
Wednesday -> add 1 day for Monday to get to the beginning on the week

Total countable days = (25 * 3) + 2 + 1 = 75 + 3 = 78 countable days

只是比“全天迭代”要快一点:

$count_only=array(1,3,5);//来自getdate()函数的天数
$start_date=1298572294;
$finish_date=1314210695;
功能日($开始日期,$结束日期,$计数日期)
{
$cnt=0;
//迭代7天
对于($deltaDays=0;$deltaDays<7;$deltaDays++)
{
$rangeStart=$start_date+$deltaDays*86400;
//检查rangeStart的工作日
$d=getDate($rangeStart);
if(在数组中($d['wday'],仅限$count_))
{
$cnt+=ceil($finish_date-$rangeStart)/604800);
}
}
返回$cnt;
}

我们的想法是使用星期一、星期二、星期三等的一些额外补偿来计算周数。

本来打算对明显重复的周数进行否决,但你却得到了对这一周数的赞成票:)最简单的方法是每天循环计算,但是你可以通过计算整周的数量并计算出剩余的天数来优化它。将
$act_day_name
转换为小写,并在数组中切换
参数的错误顺序,我将对此进行投票,但如果时差为30年,这将导致大约10万次迭代。。。它不会杀死服务器吗?在数组中完成了,不知道$act_day_name怎么不是小写的吗?好吧,可能是它减慢了服务器的速度,但我想不出其他方法,除了迭代几天,找出它是否在数组中并计算结果。也许其他人会想出一个更聪明的解决方案。@Boo
date('l')
返回第一个大写字母字符串“Sunday-thresday”很高兴看到一个没有循环的答案。哇,这真是一个不错的答案。我不确定它是否每次都是正确的(取决于四舍五入做得有多好),但这绝对是一条路要走!你能确认一下吗?我有78个。。。似乎是对的号码?你是对的-不知道那里发生了什么。删除评论。
$start_date = 1298572294;  // Tuesday
$finish_date = 1314210695; // Wednesday

$diff = 1314210695-1298572294 = 15638401 -> ~181 days -> 25.8 weeks -> 25 full weeks.
Tuesday -> add 2 days for Wednesday+Friday to get to the end of the week
Wednesday -> add 1 day for Monday to get to the beginning on the week

Total countable days = (25 * 3) + 2 + 1 = 75 + 3 = 78 countable days
$count_only = array(1, 3, 5); // days numbers from getdate() function
$start_date = 1298572294;
$finish_date = 1314210695;

function days($start_date, $finish_date, $count_only)
{
    $cnt = 0;

    // iterate over 7 days
    for ($deltaDays = 0; $deltaDays < 7; $deltaDays++)
    {
        $rangeStart = $start_date + $deltaDays * 86400;

        // check the weekday of rangeStart
        $d = getDate($rangeStart);
        if (in_array($d['wday'], $count_only))
        {
            $cnt += ceil(($finish_date - $rangeStart) / 604800);
        }
    }

    return $cnt;
}