PHP获取一年中的每星期的开始日期和结束日期_PHP教程

WBOY
풀어 주다: 2016-07-20 10:56:44
원래의
802명이 탐색했습니다.

以下是一段通过PHP来获取一年中的每星期的开始日期和结束日期的代码。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> get_week(</span><span class="vars">$year</span><span>) {   </span></span></li>
<li>
<span>    </span><span class="vars">$year_start</span><span> = </span><span class="vars">$year</span><span> . </span><span class="string">"-01-01"</span><span>;   </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$year_end</span><span> = </span><span class="vars">$year</span><span> . </span><span class="string">"-12-31"</span><span>;   </span>
</li>
<li>
<span>    </span><span class="vars">$startday</span><span> = </span><span class="func">strtotime</span><span>(</span><span class="vars">$year_start</span><span>);   </span>
</li>
<li class="alt">
<span>    </span><span class="keyword">if</span><span> (</span><span class="func">intval</span><span>(</span><span class="func">date</span><span>(</span><span class="string">'N'</span><span>, </span><span class="vars">$startday</span><span>)) != </span><span class="string">'1'</span><span>) {   </span>
</li>
<li>
<span>        </span><span class="vars">$startday</span><span> = </span><span class="func">strtotime</span><span>(</span><span class="string">"next monday"</span><span>, </span><span class="func">strtotime</span><span>(</span><span class="vars">$year_start</span><span>)); </span><span class="comment">//获取年第一周的日期  </span><span> </span>
</li>
<li class="alt"><span>    }   </span></li>
<li>
<span>    </span><span class="vars">$year_mondy</span><span> = </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d"</span><span>, </span><span class="vars">$startday</span><span>); </span><span class="comment">//获取年第一周的日期  </span><span> </span>
</li>
<li class="alt"><span>   </span></li>
<li>
<span>    </span><span class="vars">$endday</span><span> = </span><span class="func">strtotime</span><span>(</span><span class="vars">$year_end</span><span>);   </span>
</li>
<li class="alt">
<span>    </span><span class="keyword">if</span><span> (</span><span class="func">intval</span><span>(</span><span class="func">date</span><span>(</span><span class="string">'W'</span><span>, </span><span class="vars">$endday</span><span>)) == </span><span class="string">'7'</span><span>) {   </span>
</li>
<li>
<span>        </span><span class="vars">$endday</span><span> = </span><span class="func">strtotime</span><span>(</span><span class="string">"last sunday"</span><span>, </span><span class="func">strtotime</span><span>(</span><span class="vars">$year_end</span><span>));   </span>
</li>
<li class="alt"><span>    }   </span></li>
<li><span>   </span></li>
<li class="alt">
<span>    </span><span class="vars">$num</span><span> = </span><span class="func">intval</span><span>(</span><span class="func">date</span><span>(</span><span class="string">'W'</span><span>, </span><span class="vars">$endday</span><span>));   </span>
</li>
<li>
<span>    </span><span class="keyword">for</span><span> (</span><span class="vars">$i</span><span> = 1; </span><span class="vars">$i</span><span> <span class="vars">$num</span><span>; </span><span class="vars">$i</span><span>++) {   </span></span>
</li>
<li class="alt">
<span>        </span><span class="vars">$j</span><span> = </span><span class="vars">$i</span><span> -1;   </span>
</li>
<li>
<span>        </span><span class="vars">$start_date</span><span> = </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d"</span><span>, </span><span class="func">strtotime</span><span>(</span><span class="string">"$year_mondy $j week "</span><span>));   </span>
</li>
<li class="alt"><span>   </span></li>
<li>
<span>        </span><span class="vars">$end_day</span><span> = </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d"</span><span>, </span><span class="func">strtotime</span><span>(</span><span class="string">"$start_date +6 day"</span><span>));   </span>
</li>
<li class="alt"><span>   </span></li>
<li>
<span>        </span><span class="vars">$week_array</span><span>[</span><span class="vars">$i</span><span>] = </span><span class="keyword">array</span><span> (   </span>
</li>
<li class="alt">
<span>            </span><span class="func">str_replace</span><span>(</span><span class="string">"-"</span><span>,   </span>
</li>
<li>
<span>            </span><span class="string">"."</span><span>,   </span>
</li>
<li class="alt">
<span>            </span><span class="vars">$start_date</span><span>   </span>
</li>
<li>
<span>        ), </span><span class="func">str_replace</span><span>(</span><span class="string">"-"</span><span>, </span><span class="string">"."</span><span>, </span><span class="vars">$end_day</span><span>));   </span>
</li>
<li class="alt"><span>    }   </span></li>
<li>
<span>    </span><span class="keyword">return</span><span> </span><span class="vars">$week_array</span><span>;   </span>
</li>
<li class="alt"><span>}  </span></li>
</ol>
로그인 후 복사

函数get_week()通过传入参数$year年份,获取当年第一天和最后一天所在的周数,计算第一周的日期,通过循环获取每一周的第一天和最后一天的日期。最后返回是一个数组。

想得到指定周数的开始日期和结束日期,比如2011年第18周的开始日期和结束日期,代码如下:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$weeks</span><span> = get_week(2011);   </span></span></li>
<li>
<span class="func">echo</span><span> </span><span class="string">'第18周开始日期:'</span><span>.</span><span class="vars">$weeks</span><span>[18][0].</span><span class="string">''</span><span>;   </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="string">'第18周结束日期:'</span><span>.</span><span class="vars">$weeks</span><span>[18][1];  </span>
</li>
</ol>
로그인 후 복사

最后输出结果:

<ol class="dp-c">
<li class="alt"><span><span>第18周开始日期:2011.05.02   </span></span></li>
<li><span>第18周结束日期:2011.05.08  </span></li>
</ol>
로그인 후 복사

http://www.helloweba.com/view-blog-125.html


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445825.htmlTechArticle以下是一段通过PHP来获取一年中的每星期的开始日期和结束日期的代码。 function get_week( $year ){ $year_start = $year . -01-01 ; $year_end = $year . -12-...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!