PHP的日期操作 增加减少(一个月以内)

<p>date(&#39;Y-m-d&#39;, strtotime(&quot;-30 days&quot;)) ; //30天前</p><p>&lt;?php</p><p>$date1=&quot;2011-08-09&quot;;</p><p>echo date(&#39;Y-m-d&#39;,strtotime(&quot;$date1 +5 day&quot;)); //相应地,要增加月,年,将day改成month或year即可</p><p>?&gt;</p><p>输出结果:2011-08-14</p><pre class="brush:php;toolbar:false">+++++++++++++今天、昨天、明天、上一周、下一周+++++++++++++++ &lt;?php date_default_timezone_set(&#39;PRC&#39;);//默认时区 echo&quot;今天:&quot;,date(&quot;Y-m-d&quot;,time()),&quot;&lt;br&gt;&quot;; echo&quot;昨天:&quot;,date(&quot;Y-m-d&quot;,strtotime(&quot;-1day&quot;)),&quot;&lt;br&gt;&quot;; echo&quot;明天:&quot;,date(&quot;Y-m-d&quot;,strtotime(&quot;+1day&quot;)),&quot;&lt;br&gt;&quot;; echo&quot;一周后:&quot;,date(&quot;Y-m-d&quot;,strtotime(&quot;+1week&quot;)),&quot;&lt;br&gt;&quot;; echo&quot;一周零两天四小时两秒后:&quot;,date(&quot;Y-m-dG:H:s&quot;,strtotime(&quot;+1week2days4hours2seconds&quot;)),&quot;&lt;br&gt;&quot;; echo&quot;下个星期四:&quot;,date(&quot;Y-m-d&quot;,strtotime(&quot;nextThursday&quot;)),&quot;&lt;br&gt;&quot;; echo&quot;上个周一:&quot;.date(&quot;Y-m-d&quot;,strtotime(&quot;lastMonday&quot;)).&quot;&lt;br&gt;&quot;; echo&quot;一个月前:&quot;.date(&quot;Y-m-d&quot;,strtotime(&quot;lastmonth&quot;)).&quot;&lt;br&gt;&quot;; echo&quot;一个月后:&quot;.date(&quot;Y-m-d&quot;,strtotime(&quot;+1month&quot;)).&quot;&lt;br&gt;&quot;; echo&quot;十年后:&quot;.date(&quot;Y-m-d&quot;,strtotime(&quot;+10year&quot;)).&quot;&lt;br&gt;&quot;; ?&gt;</pre><p>php 本周开始时间和结束时间;本月开始时间结束时间;上月开始时间结束时间</p><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><br/></p><p>strtotime获取本周第一天和最后一天方法的BUG</p><p></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><br/></p><p>但在跨年的时候使用会有问题</p><p>例如2011年的12月31日周六和2012年1月1日周日,拿到的周一和周日完全不同</p><p>2011年12月31日拿合到的周一和周日分别对应</p><p>2011-12-26</p><p>2012-01-01</p><p></p><p>但2012年1月1日拿 到的周一和周日分别对应</p><p>2012-01-02</p><p>2012-01-04</p><p></p><p>原因为传进去的方法的周为第53周,但是年为2011年,所以认为2011的第53周,所以计算有误,解决方法为,</p><p>如果周为大于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.006338s
RangeMem:211.57 KB
返回顶部 留言