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

后端 未结 9 473
走了就别回头了
走了就别回头了 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:26

    You might want to look at the strtotime and date functions.

    <?php
    
    $query_date = '2010-02-04';
    
    // First day of the month.
    echo date('Y-m-01', strtotime($query_date));
    
    // Last day of the month.
    echo date('Y-m-t', strtotime($query_date));
    
    0 讨论(0)
  • 2020-12-08 09:28

    cal_days_in_month() should give you the total number of days in the month, and therefore, the last one.

    0 讨论(0)
  • 2020-12-08 09:29
    // First date of the current date
    echo date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y')));
    echo '<br />';
    // Last date of the current date
    echo date('Y-m-d', mktime(0, 0, 0, date('m')+1, 0, date('Y')));
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-08 09:32

    Basically:

    $lastDate = date("Y-m-t", strtotime($query_d));
    

    Date t parameter return days number in current month.

    0 讨论(0)
  • 2020-12-08 09:34
    $month = 10; // october
    
    $firstday = date('01-' . $month . '-Y');
    $lastday = date(date('t', strtotime($firstday)) .'-' . $month . '-Y');
    
    0 讨论(0)
提交回复
热议问题