首頁 > php教程 > php手册 > PHP获取指定日期所在月的开始日期与结束日期

PHP获取指定日期所在月的开始日期与结束日期

WBOY
發布: 2016-06-13 11:30:42
原創
1333 人瀏覽過


如下:

<?<span php
</span><span /*</span><span *
* 获取指定日期所在月的开始日期与结束日期
* @param string $date
* @param boolean 为true返回开始日期,否则返回结束日期
* @return array
* @access private
</span><span */</span>
<span private</span> <span function</span> getMonthRange( <span $date</span>, <span $returnFirstDay</span> = <span true</span><span  ) {
</span><span $timestamp</span> = <span strtotime</span>( <span $date</span><span  );
</span><span if</span> ( <span $returnFirstDay</span><span  ) {
</span><span $monthFirstDay</span> = <span date</span>( 'Y-m-1 00:00:00', <span $timestamp</span><span  );
</span><span return</span> <span $monthFirstDay</span><span ;
} </span><span else</span><span  {
</span><span $mdays</span> = <span date</span>( 't', <span $timestamp</span><span  );
</span><span $monthLastDay</span> = <span date</span>( 'Y-m-' . <span $mdays</span> . ' 23:59:59', <span $timestamp</span><span  );
</span><span return</span> <span $monthLastDay</span><span ;
}
}
</span>?>
登入後複製

代码:

<?<span php
</span><span //</span><span  获取指定日期所在星期的开始时间与结束时间</span>
<span function</span> getWeekRange(<span $date</span><span ){
</span><span $ret</span>=<span array</span><span ();
</span><span $timestamp</span>=<span strtotime</span>(<span $date</span><span );
</span><span $w</span>=<span strftime</span>('%u',<span $timestamp</span><span );
</span><span $ret</span>['sdate']=<span date</span>('Y-m-d 00:00:00',<span $timestamp</span>-(<span $w</span>-1)*86400<span );
</span><span $ret</span>['edate']=<span date</span>('Y-m-d 23:59:59',<span $timestamp</span>+(7-<span $w</span>)*86400<span );
</span><span return</span> <span $ret</span><span ;
} by www</span>.jbxue.<span com

</span><span //</span><span  获取指定日期所在月的开始日期与结束日期</span>
<span function</span> getMonthRange(<span $date</span><span ){
</span><span $ret</span>=<span array</span><span ();
</span><span $timestamp</span>=<span strtotime</span>(<span $date</span><span );
</span><span $mdays</span>=<span date</span>('t',<span $timestamp</span><span );
</span><span $ret</span>['sdate']=<span date</span>('Y-m-1 00:00:00',<span $timestamp</span><span );
</span><span $ret</span>['edate']=<span date</span>('Y-m-'.<span $mdays</span>.' 23:59:59',<span $timestamp</span><span );
</span><span return</span> <span $ret</span><span ;
}


</span><span //</span><span  以上两个函数的应用</span>
<span function</span> getFilter(<span $n</span><span ){
</span><span $ret</span>=<span array</span><span ();
</span><span switch</span>(<span $n</span><span ){
</span><span case</span> 1:<span //</span><span  昨天</span>
<span $ret</span>['sdate']=<span date</span>('Y-m-d 00:00:00',<span strtotime</span>('-1 day'<span ));
</span><span $ret</span>['edate']=<span date</span>('Y-m-d 23:59:59',<span strtotime</span>('-1 day'<span ));
</span><span break</span><span ;
</span><span case</span> 2:<span //</span><span 本星期</span>
<span $ret</span>=getWeekRange(<span date</span>('Y-m-d'<span ));
</span><span break</span><span ;
</span><span case</span> 3:<span //</span><span 上一个星期</span>
<span $strDate</span>=<span date</span>('Y-m-d',<span strtotime</span>('-1 week'<span ));
</span><span $ret</span>=getWeekRange(<span $strDate</span><span );
</span><span break</span><span ;
</span><span case</span> 4: <span //</span><span 上上星期</span>
<span $strDate</span>=<span date</span>('Y-m-d',<span strtotime</span>('-2 week'<span ));
</span><span $ret</span>=getWeekRange(<span $strDate</span><span );
</span><span break</span><span ;
</span><span case</span> 5: <span //</span><span 本月</span>
<span $ret</span>=getMonthRange(<span date</span>('Y-m-d'<span ));
</span><span break</span><span ;
</span><span case</span> 6:<span //</span><span 上月</span>
<span $strDate</span>=<span date</span>('Y-m-d',<span strtotime</span>('-1 month'<span ));
</span><span $ret</span>=getMonthRange(<span $strDate</span><span );
</span><span break</span><span ;
}
</span><span return</span> <span $ret</span><span ;
}</span>?>
登入後複製

 

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板