怎么获得一段时间内的完整星期的数量SegmentFault

WBOY
Release: 2016-06-06 20:19:43
Original
1444 people have browsed it

怎么获得一段时间内的完整星期(周一到周末)的数量php 有什么比较好的方法

回复内容:

怎么获得一段时间内的完整星期(周一到周末)的数量php 有什么比较好的方法

额。。。需要那么复杂吗

<code class="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');</code>
Copy after login

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

<code class="php"><?php /*  作用由起止日期算出其中的周
 *  @param start_date 开始日期
 *  @param end_date   结束日期
 *  @return 返回二维数组,含每周起止时间
 *  @author anngly
 *  @modify for segmentfault
 *  注意:end_date>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 ";
            echo "开始".$sd."<br>";
            echo "结束".$eed."<br>";
        }
        return $arr;
    }
}
var_dump(getWeek("20160101", "20160120"));
?></code>
Copy after login

输出

<code class="php">第 1 周
开始20160104
结束20160110
第 2 周
开始20160111
结束20160117
array(2) { [0]=> array(2) { [0]=> string(8) "20160104" [1]=> string(8) "20160110" } [1]=> array(2) { [0]=> string(8) "20160111" [1]=> string(8) "20160117" } }</code>
Copy after login

<code>DatePeriod</code>
Copy after login

拿到开始-结束的日期集合(摸我),然后遍历,如果:

<code>in_array(date('w', strtotime($date)), [0, 6])</code>
Copy after login

就 ++


楼主又修改了问题,真崩溃啊

<code>strtotime('next monday');</code>
Copy after login

拿到下一个周一,DatePeriod步长设为7,遍历之。

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

  • 中间取模(n/seconds_of_week),

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

这样比较中庸吧

懒得改了, 标题没看清 -- 完整

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template