计算两个日期之间的星期天 [英] calculate sundays between two dates

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

问题描述

我想计算给定两个日期之间的所有星期日.我尝试了以下代码.如果天数较少,但如果我输入的天数较多,则效果很好.它可以保持处理,并且最大执行时间超过我更改的时间,但是即使执行时间为200秒,它也可以保持处理.

I want to calculate all Sunday's between given two dates. I tried following code. It works fine if days are less but if i enter more days. It keeps processing and Maximum execution time exceeds i changed the time but it even keeps processing even execution time is 200sec.

代码是

<?php
$one="2013-01-01";
$two="2013-02-30";

$no=0;
for($i=$one;$i<=$two;$i++)
{

    $day=date("N",strtotime($i));
    if($day==7)
    {
    $no++;
    }
}
echo $no;

?>

请帮助.

推荐答案

约翰·康德(John Conde)的答案是正确的,但这是一种更有效,更数学的解决方案:

John Conde's answer is correct, but here is a more efficient and mathy solution:

$start = new DateTime('2013-01-06');
$end = new DateTime('2013-01-20');
$days = $start->diff($end, true)->days;

$sundays = intval($days / 7) + ($start->format('N') + $days % 7 >= 7);

echo $sundays;

让我为您分解一下.

$start = new DateTime('2013-01-06');
$end = new DateTime('2013-01-20');

首先,创建一些 DateTime 对象,它们是功能强大的内置对象,在PHP对象中就恰好解决了这类问题.

First, create some DateTime objects, which are powerful built-in PHP objects meant for exactly this kind of problem.

$days = $start->diff($end, true)->days;

接下来,使用 DateTime :: diff 来查找与$start$end(在此将true作为第二个参数传递,以确保该值始终为正),并获取它们之间的天数.

Next, use DateTime::diff to find the difference from $start to $end (passing true here as the second parameter ensures that this value is always positive), and get the number of days between them.

$sundays = intval($days / 7) + ($start->format('N') + $days % 7 >= 7);

这是最重要的-确实不是那么复杂.首先,我们知道每个星期都有一个星期日,因此我们至少要有$days / 7个星期日开始,然后用intval向下舍入到最接近的int.

Here comes the big one - but it's not so complicated, really. First, we know there is one Sunday for every week, so we have at least $days / 7 Sundays to begin with, rounded down to the nearest int with intval.

最重要的是,在不到一周的时间内,可能会有一个星期日;例如,下周的星期五到星期一包含4天;其中之一是星期天.因此,取决于我们开始和结束的时间,可能还会有另一个.这很容易解释:

On top of that, there could be a Sunday in a span of time less than a week; for example, Friday to Monday of the next week contains 4 days; one of them is a Sunday. So, depending on when we start and end, there could be another. This is easy to account for:

  • $start->format('N')(请参见 DateTime :: format )为我们提供了 ISO-8601 作为开始日期的星期几,即从1到7的数字(1是星期一,7是星期日).
  • $days % 7为我们提供了剩余天数,这些剩余天数不能平均分为几周.
  • $start->format('N') (see DateTime::format) gives us the ISO-8601 day of the week for the start date, which is a number from 1 to 7 (1 is Monday, 7 is Sunday).
  • $days % 7 gives us the number of leftover days that don't divide evenly into weeks.

如果我们的开始日期和剩余天数加起来等于或大于7,则我们到了星期日.知道这一点,我们只需要添加该表达式即可,如果我们将1设置为true,则为0赋值,因为我们将其添加到int值.

If our starting day and the number of leftover days add up to 7 or more, then we reached a Sunday. Knowing that, we just have to add that expression, which will give us 1 if it's true or 0 if it's false, since we're adding it to an int value.

就在那里!这种方法的优点是,它不需要在给定时间之间每天进行迭代,也不需要检查是否是星期天,这样可以节省大量计算量,也可以使您看起来很聪明.希望有帮助!

And there you have it! The advantage of this method is that it doesn't require iterating over every day between the given times and checking to see if it's a Sunday, which will save you a lot computation, and also it will make you look really clever. Hope that helps!

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

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