how to get the first and last days of a given month

后端 未结 9 475
走了就别回头了
走了就别回头了 2020-12-08 09:11

I wish to rewrite a mysql query which use month() and year() functions to display all the posts from a certain month which goes to my function as a \'Y-m-d\' parameter forma

9条回答
  •  失恋的感觉
    2020-12-08 09:31

    Print only current month week:

    function my_week_range($date) {
        $ts = strtotime($date);
        $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
        echo $currentWeek = ceil((date("d",strtotime($date)) - date("w",strtotime($date)) - 1) / 7) + 1;
        $start_date = date('Y-m-d', $start);$end_date=date('Y-m-d', strtotime('next saturday', $start));
        if($currentWeek==1)
            {$start_date = date('Y-m-01', strtotime($date));}
        else if($currentWeek==5)
           {$end_date = date('Y-m-t', strtotime($date));}
        else
           {}
        return array($start_date, $end_date );
    }
    
    $date_range=list($start_date, $end_date) = my_week_range($new_fdate);
    

提交回复
热议问题