PHP subtract 1 month from date formatted with date ('m-Y')

后端 未结 10 580
北恋
北恋 2020-12-20 11:11

I\'m trying to subtract 1 month from a date.

$today = date(\'m-Y\');

This gives: 08-2016

How can I subtract a month to get 07

相关标签:
10条回答
  • 2020-12-20 11:23

    Try this,

    $effectiveDate = date('2018-01'); <br> 
    echo 'Date'.$effectiveDate;<br>
    $effectiveDate = date('m-y', strtotime($effectiveDate.'+-1 months'));<br>
    echo 'Date'.$effectiveDate;
    
    0 讨论(0)
  • 2020-12-20 11:27
    $lastMonth = date('Y-m', strtotime('-1 MONTH'));
    
    0 讨论(0)
  • 2020-12-20 11:29
    $currentMonth = date('m', time());
    $currentDay = date('d',time());
    $currentYear = date('Y',time());
    $lastMonth = $currentMonth -1;
    $one_month_ago=mkdate(0,0,0,$one_month_ago,$currentDay,$currentYear);
    

    This could be rewritten more elegantly, but it works for me

    0 讨论(0)
  • 2020-12-20 11:34
    if(date("d") > 28){
        $date = date("Y-m", strtotime("-".$loop." months -2 Day"));
    } else {
        $date = date("Y-m", strtotime("-".$loop." months"));
    }
    
    0 讨论(0)
  • 2020-12-20 11:37
     <?php 
      echo $newdate = date("m-Y", strtotime("-1 months"));
    

    output

    07-2016
    
    0 讨论(0)
  • 2020-12-20 11:43

    Depending on your PHP version you can use DateTime object (introduced in PHP 5.2 if I remember correctly):

    <?php
    $today = new DateTime(); // This will create a DateTime object with the current date
    $today->modify('-1 month');
    

    You can pass another date to the constructor, it does not have to be the current date. More information: http://php.net/manual/en/datetime.modify.php

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