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

发布于 2024-12-01 12:07:18 字数 377 浏览 0 评论 0原文

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

我需要根据经过的天数来计算数字,例如 - 伪代码

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

有没有办法确定已经过去了多少整天,同时只计算某些天数?

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

Is there a way determine how many full days have elapsed, while only counting certain days?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜尕妞 2024-12-08 12:07:18

您可以通过计算两个指定日期之间有多少个完整周来大大简化这一过程,然后对开始/结束部分周进行一些数学计算以考虑悬空日期。

例如

$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

You can simplify this considerably by calculating how many complete weeks fall between the two specified dates, then do some math for the beginning/end partial weeks to account for dangling dates.

e.g.

$start_date = 1298572294;  // Tuesday
$finish_date = 1314210695; // Wednesday

$diff = 1314210695-1298572294 = 15638401 -> ~181 days -> 25.8 weeks -> 25 full weeks.

Then it's just a simple matter of checking for the dangling dates:

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
肩上的翅膀 2024-12-08 12:07:18

您可以创建一个循环,从 $start_date 开始转到 $count_only 数组中的第二天,并在到达 $ 时停止(从函数返回)结束日期

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

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;
        }
    }
}
自找没趣 2024-12-08 12:07:18

当然有办法:-)

过去的日子根本

$elapsed_days = floor(($finish_date-$start_date) / 86400);

就得不到你需要的结果。你可以做的是以下(伪)代码:

$elapsed_days = floor(($finish_date-$start_date) / 86400);
for(int $i=0;$i<$elapsed_days;$i++){
  $act_day_name = strtolower(date('l',$start_date+$i*86400));
  if(in_array($act_day_name,$count_only){
    // found matching day
  }
}

我做什么:
我迭代两个日期之间的每一天,用 date('l') 获取日期名称;并检查它是否在数组内。
可能需要进行一些微调,但这应该可以让您继续前进。

Of course there is a way :-)

The days that have been elapsed is simply

$elapsed_days = floor(($finish_date-$start_date) / 86400);

This will not get the result you need. What you could do is the following (pesudo)code:

$elapsed_days = floor(($finish_date-$start_date) / 86400);
for(int $i=0;$i<$elapsed_days;$i++){
  $act_day_name = strtolower(date('l',$start_date+$i*86400));
  if(in_array($act_day_name,$count_only){
    // found matching day
  }
}

What I do:
I iterate over every day which is between the both dates, get the day-name with date('l'); and check if it's within the array.
There may be some fine tuning need to be done, but this should get you going.

黯淡〆 2024-12-08 12:07:18

只是比“遍历所有日期”更快一点的方法:

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

这个想法是使用周一、周二、周三等的一些额外偏移量来计算周数。

Just a bit faster approach than "iterating through all 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;
}

The idea is to count number of weeks using some additional offsets for mondays, tuesdays, wednesdays etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文