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

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

WBOY
Release: 2016-06-13 11:30:42
Original
1326 people have browsed it


如下:

<?<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>?>
Copy after login

代码:

<?<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>?>
Copy after login

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template