38

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
  • 34,719
  • 24
  • 185
  • 251
Grant
  • 383
  • 1
  • 3
  • 4

10 Answers10

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

output

07-2016
Passionate Coder
  • 6,650
  • 2
  • 16
  • 36
15

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
  • 2,853
  • 2
  • 19
  • 31
  • 5
    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
8

Try this,

$today = date('m-Y');
$newdate = date('m-Y', strtotime('-1 months', strtotime($today))); 
echo $newdate;
Vinod VT
  • 6,680
  • 10
  • 52
  • 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
  • 922
  • 8
  • 15
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,103
  • 4
  • 20
  • 28
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
$lastMonth = date('Y-m', strtotime('-1 MONTH'));
Brennan James
  • 177
  • 1
  • 4
  • 6
    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
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,020
  • 11
  • 14
0

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,410
  • 4
  • 11
  • 22
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