计算两个日期之间的星期日

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

问题描述

限时送ChatGPT账号..

我想计算给定两个日期之间的所有星期日.我尝试了以下代码.如果天数更少,它工作正常,但如果我输入更多天数.它继续处理并且最大执行时间超过我更改的时间,但即使执行时间为 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 个星期日开始,四舍五入到最接近的 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 或者如果它是假的 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!

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

上一篇:从日期中减去一定数量的小时、天、月或年 下一篇:计算PHP中两个日期之间的工作时间

相关文章

最新文章