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

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

    Try this , if you are using PHP 5.3+, in php

    $query_date = '2010-02-04';
    $date = new DateTime($query_date);
    //First day of month
    $date->modify('first day of this month');
    $firstday= $date->format('Y-m-d');
    //Last day of month
    $date->modify('last day of this month');
    $lastday= $date->format('Y-m-d');
    

    For finding next month last date, modify as follows,

     $date->modify('last day of 1 month');
     echo $date->format('Y-m-d');
    

    and so on..

    0 讨论(0)
  • 2020-12-08 09:44

    I know this question has a good answer with 't', but thought I would add another solution.

    $first = date("Y-m-d", strtotime("first day of this month"));
    $last = date("Y-m-d", strtotime("last day of this month"));
    
    0 讨论(0)
  • 2020-12-08 09:46
    ## Get Current Month's First Date And Last Date
    
    echo "Today Date: ". $query_date = date('d-m-Y');
    echo "<br> First day of the month: ". date('01-m-Y', strtotime($query_date));
    echo "<br> Last day of the month: ". date('t-m-Y', strtotime($query_date));
    
    0 讨论(0)
提交回复
热议问题