PHP擷取當前日期所在星期(月份)的開始日期與結束日期

來源:互聯網
上載者:User
 1 //author:zhxia 擷取指定日期所在星期的開始時間與結束時間
 2 function getWeekRange($date){
 3     $ret=array();
 4     $timestamp=strtotime($date);
 5     $w=strftime('%u',$timestamp);
 6     $ret['sdate']=date('Y-m-d 00:00:00',$timestamp-($w-1)*86400);
 7     $ret['edate']=date('Y-m-d 23:59:59',$timestamp+(7-$w)*86400);
 8     return $ret;
 9 }
10 
11 //author:zhxia 擷取指定日期所在月的開始日期與結束日期
12 function getMonthRange($date){
13     $ret=array();
14     $timestamp=strtotime($date);
15     $mdays=date('t',$timestamp);
16     $ret['sdate']=date('Y-m-1 00:00:00',$timestamp);
17     $ret['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp);
18     return $ret;
19 }
20 
21 
22 //author:zhxia  以上兩個函數的應用
23 function getFilter($n){
24     $ret=array();
25     switch($n){
26         case 1:// 昨天
27             $ret['sdate']=date('Y-m-d 00:00:00',strtotime('-1 day'));
28             $ret['edate']=date('Y-m-d 23:59:59',strtotime('-1 day'));
29         break;
30         case 2://本星期
31             $ret=getWeekRange(date('Y-m-d'));
32         break;
33         case 3://上一個星期
34             $strDate=date('Y-m-d',strtotime('-1 week'));
35             $ret=getWeekRange($strDate);
36         break;
37         case 4: //上上星期
38             $strDate=date('Y-m-d',strtotime('-2 week'));
39             $ret=getWeekRange($strDate);
40         break;
41         case 5: //本月
42             $ret=getMonthRange(date('Y-m-d'));
43             break;
44         case 6://上月
45             $strDate=date('Y-m-d',strtotime('-1 month'));
46             $ret=getMonthRange($strDate);
47         break;
48     }
49     return $ret;
50 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.