Hướng dẫn minus month php

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

35.5k24 gold badges188 silver badges257 bronze badges

asked Aug 3, 2016 at 8:55

3

 

output

07-2016

answered Aug 3, 2016 at 9:00

Passionate CoderPassionate Coder

7,0262 gold badges17 silver badges37 bronze badges

4

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

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

answered Oct 10, 2017 at 12:54

Alexey KosovAlexey Kosov

2,8832 gold badges20 silver badges31 bronze badges

1

Try this,

$today = date('m-Y');
$newdate = date('m-Y', strtotime('-1 months', strtotime($today))); 
echo $newdate;

answered Aug 3, 2016 at 9:00

Hướng dẫn minus month php

Vinod VTVinod VT

6,69010 gold badges52 silver badges74 bronze badges

3

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

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

answered Aug 3, 2016 at 9:09

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:

answered Jul 31, 2020 at 10:31

Hướng dẫn minus month php

MarcoMarco

3,2544 gold badges22 silver badges29 bronze badges

if(date("d") > 28){
    $date = date("Y-m", strtotime("-".$loop." months -2 Day"));
} else {
    $date = date("Y-m", strtotime("-".$loop." months"));
}

answered May 9, 2019 at 6:00

Hướng dẫn minus month php

1

$lastMonth = date('Y-m', strtotime('-1 MONTH'));

answered Jun 15, 2019 at 20:29

Hướng dẫn minus month php

1

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');

answered Jun 25, 2019 at 7:35

Hướng dẫn minus month php

AngularJMKAngularJMK

1,08013 silver badges14 bronze badges

Try this,

$effectiveDate = date('2018-01'); 
echo 'Date'.$effectiveDate;
$effectiveDate = date('m-y', strtotime($effectiveDate.'+-1 months'));
echo 'Date'.$effectiveDate;

Hướng dẫn minus month php

Android

1,4104 gold badges11 silver badges23 bronze badges

answered Jun 26, 2019 at 10:08

Hướng dẫn minus month php

$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

answered Dec 29, 2019 at 7:53

1