Getting last month's date in php

前端 未结 18 702
心在旅途
心在旅途 2020-11-29 00:22

I want to get last month\'s date. I wrote this out:

$prevmonth = date(\'M Y\');

Which gives me the current month/year. I can\'t tell if I

相关标签:
18条回答
  • 2020-11-29 00:51

    Simply get last month.

    Example:

    Today is: 2020-09-02

    Code:

    echo date('Y-m-d', strtotime(date('Y-m-d')." -1 month"));
    

    Result:

    2020-08-02

    0 讨论(0)
  • 2020-11-29 00:52
    $prevmonth = date('M Y', strtotime("last month"));
    
    0 讨论(0)
  • 2020-11-29 00:52

    if you want to get just previous month, then you can use as like following

    $prevmonth = date('M Y', strtotime('-1 months'));
    

    if you want to get same days of previous month, Then you can use as like following ..

    $prevmonth = date('M Y d', strtotime('-1 months'));
    

    if you want to get last date of previous month , Then you can use as like following ...

    $prevmonth = date('M Y t', strtotime('-1 months'));
    

    if you want to get first date of previous month , Then you can use as like following ...

    $prevmonth = date('M Y 1', strtotime('-1 months'));
    
    0 讨论(0)
  • 2020-11-29 00:54

    It works for me:

    Today is: 31/03/2012

    echo date("Y-m-d", strtotime(date('m', mktime() - 31*3600*24).'/01/'.date('Y').' 00:00:00')); // 2012-02-01
    echo  date("Y-m-d", mktime() - 31*3600*24); // 2012-02-29
    
    0 讨论(0)
  • 2020-11-29 00:55
    public function getLastMonth() {
        $now = new DateTime();
        $lastMonth = $now->sub(new DateInterval('P1M'));
        return $lastMonth->format('Ym');
    }
    
    0 讨论(0)
  • 2020-11-29 00:55
    $lastMonth = date('M Y', strtotime("-1 month"));
    var_dump($lastMonth);
    $lastMonth = date('M Y', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
    var_dump($lastMonth);
    
    0 讨论(0)
提交回复
热议问题