本文实例讲述了PHP实现获取某个月份周次信息的方法。分享给大家供大家参考。具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?php function getMonthweeks( $date ){ $ret = array (); $stimestamp = strtotime ( $date ); $mdays = date ( 't' , $stimestamp ); $msdate = date ( 'Y-m-d' , $stimestamp ); $medate = date ( 'Y-m-' . $mdays , $stimestamp ); $etimestamp = strtotime ( $medate ); //獲取第一周 $zcsy =6- date ( 'w' , $stimestamp ); //第一周去掉第一天還有幾天 $zcs1 = $msdate ; $zce1 = date ( 'Y-m-d' , strtotime ( "+$zcsy day" , $stimestamp )); $ret [1]= "第1周" . $zcs1 . '~' . $zce1 ; //獲取中間周次 $jzc =0; //獲得當前月份是6周次還是5周次 $jzc0 = "" ; $jzc6 = "" ; for ( $i = $stimestamp ; $i <= $etimestamp ; $i +=86400){ if ( date ( 'w' , $i ) == 0){ $jzc0 ++;} if ( date ( 'w' , $i ) == 6){ $jzc6 ++;} } if ( $jzc0 ==5 && $jzc6 ==5) { $jzc =5; } else { $jzc =4; } date_default_timezone_set( 'PRC' ); $t = strtotime ( '+1 monday ' . $msdate ); $n = 1; for ( $n =1; $n < $jzc ; $n ++) { $b = strtotime ( "+$n week -1 week" , $t ); $dsdate = date ( "Y-m-d" , strtotime ( "-1 day" , $b )); $dedate = date ( "Y-m-d" , strtotime ( "5 day" , $b )); $jzcz = $n +1; $ret [ $jzcz ]= "第" . $jzcz . "周" . $dsdate . '~' . $dedate ; } //獲取最後一周 $zcsy = date ( 'w' , $etimestamp ); //最後一周是周幾日~六 0~6 $zcs1 = date ( 'Y-m-d' , strtotime ( "-$zcsy day" , $etimestamp )); $zce1 = $medate ; $jzcz = $jzc +1; $ret [ $jzcz ]= "第" . $jzcz . "周" . $zcs1 . '~' . $zce1 ; return $ret ; } $ret =getMonthweeks( '2012-12-01' ); for ( $i =0; $i <= count ( $ret ); $i ++) { echo @ $ret [ $i ]. "<br />" ; } ?> |
运行结果如下:
第1周2012-12-01~2012-12-01
第2周2012-12-02~2012-12-08
第3周2012-12-09~2012-12-15
第4周2012-12-16~2012-12-22
第5周2012-12-23~2012-12-29
第6周2012-12-30~2012-12-31
希望本文所述对大家的php程序设计有所帮助。