首页 > 后端开发 > php教程 > PHP获取指定日期所在月的开始日期与结束日期_PHP教程

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

WBOY
发布: 2016-07-20 11:14:58
原创
1334 人浏览过


如下:

<?<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>?>
登录后复制

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440237.htmlTechArticle如下: ? getMonthRange( , = = ( ( = ( 'Y-m-1 00:00:00', = ( 't', = ( 'Y-m-' . . ' 23:59:59', ? 代码: ? getWeekRange( = = ( = ('%u', ['sdate']= ('Y-m-d 00:00:00', -( -1)*8640...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板