以下は、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><span class="vars">$num</span><span>; </span><span class="vars">$i</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