問題描述
我想計(jì)算給定兩個(gè)日期之間的所有星期日.我嘗試了以下代碼.如果天數(shù)更少,它工作正常,但如果我輸入更多天數(shù).它繼續(xù)處理并且最大執(zhí)行時(shí)間超過我更改的時(shí)間,但即使執(zhí)行時(shí)間為 200 秒它甚至繼續(xù)處理.
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 的回答是正確的,但這里有一個(gè)更有效和更數(shù)學(xué)的解決方案:
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');
首先,創(chuàng)建一些 DateTime 對象,它們是強(qiáng)大的內(nèi)置對象——在 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
作為第二個(gè)參數(shù)確保這個(gè)值總是正數(shù)),并得到之間的天數(shù)他們.
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);
重要的事情來了——但實(shí)際上并沒有那么復(fù)雜.首先,我們知道每周有一個(gè)星期日,所以我們至少有 $days/7
個(gè)星期日開始,四舍五入到最接近的 int
和 整數(shù)
.
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
.
最重要的是,在不到一周的時(shí)間內(nèi)可能會(huì)有一個(gè)星期天;例如,下周的周五到周一包含 4 天;其中之一是星期天.所以,根據(jù)我們開始和結(jié)束的時(shí)間,可能會(huì)有另一個(gè).這很容易解釋:
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 之間的數(shù)字(1 是星期一,7 是星期日).$days % 7
為我們提供了不均勻劃分為周的剩余天數(shù).
$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.
如果我們的起始日和剩余天數(shù)加起來等于或大于 7,那么我們就到達(dá)了星期日.知道了這一點(diǎn),我們只需要添加那個(gè)表達(dá)式,如果它是真的,它會(huì)給我們 1
或者如果它是假的 0
,因?yàn)槲覀儗⑺砑拥?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.
這就給你了!這種方法的優(yōu)點(diǎn)是不需要在給定的時(shí)間之間每天迭代并檢查是否是星期日,這將為您節(jié)省大量計(jì)算,并且它會(huì)使您看起來非常聰明.希望有幫助!
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!
這篇關(guān)于計(jì)算兩個(gè)日期之間的星期日的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!