43

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-2016?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Grant
  • 433
  • 1
  • 4
  • 4

11 Answers11

79
 <?php 
  echo $newdate = date("m-Y", strtotime("-1 months"));

output

07-2016
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
21

Warning! The above-mentioned examples won't work if call them at the end of a month.

<?php
$now = mktime(0, 0, 0, 10, 31, 2017);
echo date("m-Y", $now)."\n";
echo date("m-Y", strtotime("-1 months", $now))."\n";

will output:

10-2017
10-2017

The following example will produce the same result:

$date = new DateTime('2017-10-31 00:00:00');
echo $date->format('m-Y')."\n";
$date->modify('-1 month');
echo $date->format('m-Y')."\n";

Plenty of ways how to solve the issue can be found in another thread: PHP DateTime::modify adding and subtracting months

Alexey Kosov
  • 3,010
  • 2
  • 23
  • 32
  • 6
    The reason behind it does not work is because the following: What's last month of `2017-10-29`? `2017-09-29`. And last month of `2017-10-30`? `2017-09-30`. And what about last month of `2017-10-31`? In theory `2017-09-31`, but this day does not exist, and then it maps onto `2017-10-01`. – Xavi Montero Jul 16 '18 at 07:37
  • The problem stated above by @XaviMontero can be solved with: `$date->modify('first day of -1 month');` – EAmez Jan 04 '23 at 15:21
9

Try this,

$today = date('m-Y');
$newdate = date('m-Y', strtotime('-1 months', strtotime($today))); 
echo $newdate;
Vinod VT
  • 6,946
  • 11
  • 51
  • 75
8

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

Jakub Krawczyk
  • 950
  • 8
  • 16
1
$lastMonth = date('Y-m', strtotime('-1 MONTH'));
Brennan James
  • 332
  • 2
  • 7
  • 7
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Jun 16 '19 at 00:55
1

Try this,

$effectiveDate = date('2018-01'); <br> 
echo 'Date'.$effectiveDate;<br>
$effectiveDate = date('m-y', strtotime($effectiveDate.'+-1 months'));<br>
echo 'Date'.$effectiveDate;
Android
  • 1,420
  • 4
  • 13
  • 23
ChinmayW
  • 11
  • 1
1

I used this to prevent the "last days of month"-error. I just use a second strtotime() to set the date to the first day of the month:

<?php
echo $newdate = date("m-Y", strtotime("-1 months", strtotime(date("Y-m")."-01")));
Marco
  • 3,470
  • 4
  • 23
  • 35
0
if(date("d") > 28){
    $date = date("Y-m", strtotime("-".$loop." months -2 Day"));
} else {
    $date = date("Y-m", strtotime("-".$loop." months"));
}
Anilbk
  • 1
0

First change the date format m-Y to Y-m

    $date = $_POST('date'); // Post month
    or
    $date = date('m-Y'); // currrent month

    $date_txt = date_create_from_format('m-Y', $date);
    $change_format = date_format($date_txt, 'Y-m');

This code minus 1 month to the given date

    $final_date = new DateTime($change_format);
    $final_date->modify('-1 month');
    $output = $final_date->format('m-Y');
AngularJMK
  • 1,178
  • 13
  • 15
0
$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

I realize this is an old post, but I've been solving the same issue, and here is what I came up with to account for all the variability. This function is just trying to get relative dates, so same day of prior month, or last day of month if you are on the last day, regardless of exactly how many days a month has. So goal is given '2010-03-31' and subtract a month, we should output '2010-02-28'.

private function subtractRelativeMonth(DateTime $incomingDate): DateTime
{
    $year = $incomingDate->format('Y');
    $month = $incomingDate->format('m');
    $day = $incomingDate->format('d');
    $daysInOldMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    if ($month == 1) { //It's January, so we have to go back to December of prior year
        $month = 12;
        $year--;
    } else {
        $month--;
    }
    $daysInNewMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    if ($day > $daysInNewMonth && $month == 2) { //New month is Feb
        $day = $daysInNewMonth;
    }
    if ($day > 29 && $daysInOldMonth > $daysInNewMonth) {
        $day = $daysInNewMonth;
    }
    $adjustedDate = new \DateTime($year . '-' . $month . '-' . $day);
    return $adjustedDate;
}
Ryan Pyeatt
  • 113
  • 6