怎麼獲得一段時間內的完整星期的數量SegmentFault

來源:互聯網
上載者:User
怎麼獲得一段時間內的完整星期(周一到周末)的數量php 有什麼比較好的方法

回複內容:

怎麼獲得一段時間內的完整星期(周一到周末)的數量php 有什麼比較好的方法

額。。。需要那麼複雜嗎

function get_weekend_days($start, $end) {    $days = (strtotime($end) - strtotime($start)) / 86400;    return floor($days / 7);}echo get_weekend_days('2014-02-03', '2015-12-28');

update 2016/01/20 13:34:07
Code

state_date **/function getWeek($start_date, $end_date){    //設定每周開始時間  monday-sunday    $week_start = "monday";    //設定返回時間格式    $date_format = "Ymd";    $start_date = strtotime($start_date);    $end_date = strtotime($end_date);    if ($start_date <= $end_date) {        $end_date = strtotime("last ".$week_start, $end_date);        $start_date = date("w", $start_date) == 1 ? $start_date : strtotime("next ".$week_start, $start_date);        $count_week = ($end_date - $start_date) / (7 * 24 * 3600);        for ($i = 0; $i < $count_week; $i++) {            $sd = date($date_format, $start_date);            $ed = strtotime("+ 6 days", $start_date);            $eed = date($date_format, $ed);            $arr[] = array($sd, $eed);            $start_date = strtotime("+ 1 day", $ed);            //輸出測試            echo "第 ".($i + 1)." 周
"; echo "開始".$sd."
"; echo "結束".$eed."
"; } return $arr; }}var_dump(getWeek("20160101", "20160120"));?>

輸出

第 1 周開始20160104結束20160110第 2 周開始20160111結束20160117array(2) { [0]=> array(2) { [0]=> string(8) "20160104" [1]=> string(8) "20160110" } [1]=> array(2) { [0]=> string(8) "20160111" [1]=> string(8) "20160117" } }

DatePeriod

拿到開始-結束的日期集合(摸我),然後遍曆,如果:

in_array(date('w', strtotime($date)), [0, 6])

就 ++

樓主又修改了問題,真崩潰啊

strtotime('next monday');

拿到下一個周一,DatePeriod步長設為7,遍曆之。

  • 掐頭(all_duration_seconds - ( ( 7 - ( day_of_week_at_begin -1 ) ) * seconds_of_day ) ),

  • 中間模數(n/seconds_of_week),

  • 去尾( rest_seconds > 0 ? 1 : 0 )

這樣比較中庸吧

懶得改了, 標題沒看清 -- 完整

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.