Search code examples
phpdatetimedatestrtotimemktime

Get previous month date range


I need help getting the previous months full date range in the following format: Y-m-d

I have successfully been able to get "this" months full date range but not the "previous" months full date range.

Any help is greatly appreciated!


Solution

  •     $last_month_first_day=strtotime('first day of last month');
        $no_of_days=date('t',$last_month_first_day);
        $date_value=$last_month_first_day;
        for($i=0;$i<$no_of_days;$i++)
        {
            echo date('Y-m-d',$date_value)."<br>";
            $date_value=strtotime("+1 day",$date_value);
        }
    

    This code will print what you want..

    First Date:

    echo date('Y-m-d',strtotime('first day of last month'));
    

    Last date:

    echo date('Y-m-d',strtotime('last day of last month'));