DeTechn Blog

PHP 获取某段时间内所有的周的方法,返回有开始时间到结束时间所有周

<?php
    /**
     * 获取某段时间内所有的周的方法,返回有开始时间到结束时间所有周(1-6)
     * @param $start_date
     * @param $end_date
     * @return array|bool
     */
    function getWeekFromRange($start_date,&nbsp;$end_date) {
        $start_timestamp&nbsp;=&nbsp;strtotime&nbsp;(&nbsp;date&nbsp;(&nbsp;&#39;Y-m-d&#39;&nbsp;.&nbsp;&#39;&nbsp;00:00:00&#39;,&nbsp;$start_date ) );
        $end_timestamp&nbsp;=&nbsp;strtotime&nbsp;(&nbsp;date&nbsp;(&nbsp;&#39;Y-m-d&#39;&nbsp;.&nbsp;&#39;&nbsp;23:59:59&#39;,&nbsp;$end_date) );
        if ($start_timestamp&gt;=$end_timestamp) return false;
        $weeks&nbsp;=&nbsp;floor&nbsp;(&nbsp;($end_timestamp - $start_timestamp) / (86400 * 7) );
        $week = array ();
        for($i&nbsp;=&nbsp;0;&nbsp;$i <= $weeks;&nbsp;$i ++) {
            if ($i == 0) {
                $startday&nbsp;=&nbsp;date&nbsp;(&nbsp;&#39;Y-m-d&#39;&nbsp;.&nbsp;&#39;&nbsp;00:00:00&#39;,&nbsp;$start_timestamp );
                $endday&nbsp;=&nbsp;date&nbsp;(&nbsp;&quot;Y-m-d&nbsp;H:i:s&quot;,&nbsp;mktime&nbsp;(&nbsp;23,&nbsp;59,&nbsp;59,&nbsp;date&nbsp;(&nbsp;&quot;m&quot;,&nbsp;$start_timestamp ), date ( "d", $start_timestamp&nbsp;)&nbsp;-&nbsp;date&nbsp;(&nbsp;&quot;w&quot;,&nbsp;$start_timestamp ) + 7, date ( "Y", $start_timestamp ) ) );
            } elseif ($i&nbsp;==&nbsp;$weeks) {
                $startday&nbsp;=&nbsp;date&nbsp;(&nbsp;&quot;Y-m-d&nbsp;H:i:s&quot;,&nbsp;mktime&nbsp;(&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;date&nbsp;(&nbsp;&quot;m&quot;,&nbsp;$start_timestamp ), date ( "d", $start_timestamp&nbsp;)&nbsp;-&nbsp;date&nbsp;(&nbsp;&quot;w&quot;,&nbsp;$start_timestamp ) + 1, date ( "Y", $start_timestamp ) ) );
                $endday&nbsp;=&nbsp;date&nbsp;(&nbsp;&#39;Y-m-d&#39;&nbsp;.&nbsp;&#39;&nbsp;00:00:00&#39;,&nbsp;$end_timestamp );
            } else {
                $startday=&nbsp;date(&quot;Y-m-d&nbsp;H:i:s&quot;,mktime(0,&nbsp;0&nbsp;,&nbsp;0,date(&quot;m&quot;,$start_timestamp),date("d",$start_timestamp)-date(&quot;w&quot;,$start_timestamp)+1,date("Y",$start_timestamp)));
                $endday&nbsp;=&nbsp;date(&quot;Y-m-d&nbsp;H:i:s&quot;,mktime(23,59,59,date(&quot;m&quot;,$start_timestamp),date("d",$start_timestamp)-date(&quot;w&quot;,$start_timestamp)+7,date("Y",$start_timestamp)));
            }
            $start_timestamp += 86400 * 7;
            $week&nbsp;[$i] = array (
                $startday,
                $endday
            );
        }
        return $week;
    }
?>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »