-2

all

Decrease month by one in strtotime in this loop.

$twitter_val7 .='{
                            date: new Date('.date("Y",strtotime($date)).', '.date("m",strtotime($date)).', '.date("d",strtotime($date)).'),
                            value: '.$result_twitter->counts.'
                            },';
Ajay Patel
  • 5,298
  • 11
  • 55
  • 87

3 Answers3

6

To decrease by 1 month using strtotime() you literally tell it to decrease by one month:

strtotime($date . ' - 1 month');

Assuming, of course, that $date is a format strtotime() understands.

Michael
  • 11,912
  • 6
  • 49
  • 64
  • 10
    Look out with this function!!! It doesn't really subtract 1 month but 30 days. 31 May will end up as 01 May – RJD22 May 31 '12 at 12:05
  • @RJD22 it's not substract 30 day. Real logic is 1) it decrease month number. From `2021-03-30` it become `2021-02-30` 2) then because `February` last day is 28th (and difference between 30 and 28 is 2 day) - it takes `2021-02-28` add to it `2 day` and now you have result `strtotime('2021-03-30 - 1 month') = '2021-03-02'` – krylov123 Apr 01 '21 at 08:37
3

I would do it like this .. i guess $date would go in place of '2000-01-01':

$initial = new DateTime('2000-01-01');
$interval = new DateInterval('P1M');
$newdate = $initial->sub( $interval );
echo $newdate->format('Y-m-d H:i:s');
tereško
  • 58,060
  • 25
  • 98
  • 150
1

To decrease month by using strtotime

$date='2014-09-03';
$numMonth=1;//here you can pass no. of month
$resultDate=date('Y-m-d',strtotime($date . " - $numMonth month"));
vineet
  • 13,832
  • 10
  • 56
  • 76
  • Please first understand the question, 6 vote answer question never be wrong – Ajay Patel Sep 04 '14 at 07:06
  • 1
    i am not say 6 vote answer wrong but it is another way to decrease month. $numMonth use as dynamic and return date as yyyy-mm-dd format. – vineet Sep 24 '14 at 09:53