久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

計(jì)算兩個(gè)日期之間的星期日

calculate sundays between two dates(計(jì)算兩個(gè)日期之間的星期日)
本文介紹了計(jì)算兩個(gè)日期之間的星期日的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我想計(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 欧美a在线 | 久久久久国产精品一区二区 | 特黄一级 | 波多野结衣先锋影音 | 国产精品久久国产精品 | 亚洲精品久久久久久久久久久 | 精品久久99 | 久久99蜜桃综合影院免费观看 | 国产成人精品久久二区二区91 | 天天操夜夜爽 | 欧美日韩一区二区在线 | julia中文字幕久久一区二区 | 亚洲成人福利在线观看 | 免费一区二区 | 成人在线视频网址 | 成人国产综合 | 精品视频久久久 | 久久久久久久久久久一区二区 | 国产探花在线精品一区二区 | 国内精品久久久久久影视8 最新黄色在线观看 | 精品欧美一区免费观看α√ | 99reav| 欧美成人h版在线观看 | 欧美一级二级在线观看 | 给我免费的视频在线观看 | 精品久久久久一区二区国产 | 玖玖国产 | av黄色在线 | 91亚洲国产精品 | 久久男人| 亚洲欧美另类在线观看 | 亚洲免费视频一区 | 91在线精品秘密一区二区 | 一区二区av在线 | 91xxx在线观看 | 天天操综合网站 | 免费看黄色国产 | 99热在这里只有精品 | 国产一区二区在线播放 | 日韩av一区二区在线观看 | 亚洲一区在线播放 |