<pre class="brush:php;toolbar:false">&lt;?php /** *功能:取得给定日期所在周的开始日期和结束日期 *参数:$gdate日期,默认为当天,格式:YYYY-MM-DD *$first一周以星期一还是星期天开始,0为星期天,1为星期一 *返回:数组array(&quot;开始日期&quot;,&quot;结束日期&quot;); * */ functionaweek($gdate=&quot;&quot;,$first=0){ if(!$gdate)$gdate=date(&quot;Y-m-d&quot;); $w=date(&quot;w&quot;,strtotime($gdate));//取得一周的第几天,星期天开始0-6 $dn=$w?$w-$first:6;//要减去的天数 //本周开始日期 $st=date(&quot;Y-m-d&quot;,strtotime(&quot;$gdate-&quot;.$dn.&quot;days&quot;)); //本周结束日期 $en=date(&quot;Y-m-d&quot;,strtotime(&quot;$st+6days&quot;)); //上周开始日期 $last_st=date(&#39;Y-m-d&#39;,strtotime(&quot;$st-7days&quot;)); //上周结束日期 $last_en=date(&#39;Y-m-d&#39;,strtotime(&quot;$st-1days&quot;)); returnarray($st,$en,$last_st,$last_en);//返回开始和结束日期 } echoimplode(&quot;|&quot;,aweek(&quot;&quot;,1)).&#39;&lt;br/&gt;&#39;; //echodate(&quot;Y-m-d&quot;,strtotime(&quot;time()&quot;)); echo&#39;本周第一天(星期日为一周开始):&#39;.date(&#39;Y-m-d&#39;,time()-86400*date(&#39;w&#39;)).&#39;&lt;br/&gt;&#39;; echo&#39;本周第一天(星期一为一周开始):&#39;.date(&#39;Y-m-d&#39;,time()-86400*date(&#39;w&#39;)+(date(&#39;w&#39;)&gt;0?86400:-6*86400)).&#39;&lt;br/&gt;&#39;; echo&#39;本月第一天:&#39;.date(&#39;Y-m-d&#39;,mktime(0,0,0,date(&#39;m&#39;),1,date(&#39;Y&#39;))).&#39;&lt;br/&gt;&#39;; echo&#39;本月最后一天:&#39;.date(&#39;Y-m-d&#39;,mktime(0,0,0,date(&#39;m&#39;),date(&#39;t&#39;),date(&#39;Y&#39;))).&#39;&lt;br/&gt;&#39;; //上个月的开始日期 $m=date(&#39;Y-m-d&#39;,mktime(0,0,0,date(&#39;m&#39;)-1,1,date(&#39;Y&#39;))); //上个月共多少天 $t=date(&#39;t&#39;,strtotime(&quot;$m&quot;)); echo&#39;上月第一天:&#39;.date(&#39;Y-m-d&#39;,mktime(0,0,0,date(&#39;m&#39;)-1,1,date(&#39;Y&#39;))).&#39;&lt;br/&gt;&#39;; echo&#39;上月最后一天:&#39;.date(&#39;Y-m-d&#39;,mktime(0,0,0,date(&#39;m&#39;)-1,$t,date(&#39;Y&#39;))).&#39;&lt;br/&gt;&#39;; ?&gt;</pre><p>PHP手册上有一个这个方法,用来返回指定日期的周一和周日</p><pre class="brush:php;toolbar:false">&lt;?php functionget_week_range($week,$year) { $timestamp=mktime(1,0,0,1,1,$year); $firstday=date(&quot;N&quot;,$timestamp); if($firstday&gt;4) $firstweek=strtotime(&#39;+&#39;.(8-$firstday).&#39;days&#39;,$timestamp); else $firstweek=strtotime(&#39;-&#39;.($firstday-1).&#39;days&#39;,$timestamp); $monday=strtotime(&#39;+&#39;.($week-1).&#39;week&#39;,$firstweek); $sunday=strtotime(&#39;+6days&#39;,$monday); $start=date(&quot;Y-m-d&quot;,$monday); $end=date(&quot;Y-m-d&quot;,$sunday); returnarray($start,$end); } ?&gt;</pre><p>strtotime获取本周第一天和最后一天方法的BUG</p><p>PHP手册上有一个这个方法,用来返回指定日期的周一和周日</p><pre class="brush:php;toolbar:false">&lt;?php functionget_week_range($week,$year) { $timestamp=mktime(1,0,0,1,1,$year); $firstday=date(&quot;N&quot;,$timestamp); if($firstday&gt;4) $firstweek=strtotime(&#39;+&#39;.(8-$firstday).&#39;days&#39;,$timestamp); else $firstweek=strtotime(&#39;-&#39;.($firstday-1).&#39;days&#39;,$timestamp); $monday=strtotime(&#39;+&#39;.($week-1).&#39;week&#39;,$firstweek); $sunday=strtotime(&#39;+6days&#39;,$monday); $start=date(&quot;Y-m-d&quot;,$monday); $end=date(&quot;Y-m-d&quot;,$sunday); returnarray($start,$end); } ?&gt;</pre><p>但在跨年的时候使用会有问题</p><p>例如2009年的12月31日周四和2010年1月1日周五周拿到的周一和周日完全不同</p><p>2009年12月31日拿合到的周一和周日分别对应</p><p>2009-12-28</p><p>2010-01-03</p><p>但2010年1月1日拿 到的周一和周日分别对应</p><p>2011-01-03</p><p>2011-01-09</p><p>原因为传进去的方法的周为第53周,但是年为2010年,所以认为2010的第53周,所以计算有误,解决方法为,如果周为大于10(因为一月个月不可能有10周),且月份为1的时候,将年减1处理</p><pre class="brush:php;toolbar:false">if(date(&#39;m&#39;,$last_week_time)==&#39;01&#39;and$tmp_last_week&gt;10) { $last_week_year--; }</pre>
RangeTime:0.008582s
RangeMem:211.64 KB
返回顶部 留言