PHP计算2个日期之间的天数 [英] PHP calculating number of days between 2 dates

查看:393
本文介绍了PHP计算2个日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发围绕日期的Web应用程序。

I am developing a web application which revolves around dates.

我需要根据几天的时间来计算数字,例如 - 伪代码

I need to calculate numbers based around days elasped, for example - pseudo code

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

有没有办法确定已经过了多少天,而只计算某些日子?

推荐答案

您可以创建一个循环,到 $ count_only中的第二天数组,从 $ start_date 并在达到 $ end_date

You could create a loop which goes to the next day in the $count_only array, from the $start_date and stopping (returning from the function) upon reaching the $end_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;
        }
    }
}

这篇关于PHP计算2个日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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