Given a time, how can I find the time one month ago

后端 未结 7 1461
梦如初夏
梦如初夏 2021-01-13 08:16

Given a time, how can I find the time one month ago.

相关标签:
7条回答
  • 2021-01-13 08:30

    These answers were driving me nuts. You can't subtract 31 days and have a sane result without skipping short months. I'm presuming you only care about the month, not the day of the month, for a case like filtering/grouping things by year and month.

    I do something like this:

    $current_ym = date('ym',strtotime("-15 days",$ts));

    0 讨论(0)
  • 2021-01-13 08:33

    We can achieve same by using PHP's modern date handling. This will require PHP 5.2 or better.

    // say its "2015-11-17 03:27:22"
    $dtTm = new DateTime('-1 MONTH', new DateTimeZone('America/Los_Angeles')); // first argument uses strtotime parsing
    echo $dtTm->format('Y-m-d H:i:s'); // "2015-10-17 03:27:22"
    

    Hope this adds some more info for this question.

    0 讨论(0)
  • 2021-01-13 08:41

    PHP 5.2=<

    $date = new DateTime(); // Return Datetime object for current time
    $date->modify('-1 month'); // Modify to deduct a month (Also can use '+1 day', '-2 day', ..etc)
    echo $date->format('Y-m-d'); // To set the format 
    

    Ref: http://php.net/manual/en/datetime.modify.php

    0 讨论(0)
  • 2021-01-13 08:42

    This code is for getting 1 month before not 30 days

    $date = "2016-03-31";
    
    $days = date("t", strtotime($date));
    
    echo  date("Y-m-d", strtotime( "-$days days", strtotime($date) ));
    
    0 讨论(0)
  • 2021-01-13 08:43
    strtotime( '-1 month', $timestamp );
    

    http://php.net/manual/en/function.strtotime.php

    0 讨论(0)
  • 2021-01-13 08:46

    In php you can use strtotime("-1 month"). Check out the documentation here: http://ca3.php.net/strtotime

    0 讨论(0)
提交回复
热议问题