-1

I know that date("Y"), date("m") and date("d") will return current Year (2013), Month (07) and Date (11) respectively.

I am working with date Format: "2013-07-11". I have current date like this. Now I want to get the value "2013-06-11" and "2013-08-11" somehow using PHP.

What might be the code to get this values (Last Month's Same Date, and Next Month's Same Date)?

I tried:

$LastMonth = date ("m") - 1;
$LastDate = date("Y") . "-0" . $LastMonth . "-" . date("d");

But this will return error when it is October. In October it will show "2013-010-11".

What can be a better solution? Can anyone help me?

Nirjhor
  • 73
  • 2
  • 4
  • 9
  • Hi, for future reference, please always remember to do a quick Google first. Searching for the title of your question leads to perfect answers, e.g. [this one.](http://stackoverflow.com/questions/14102625/php-strtotime-get-previous-month) Thanks! – Pekka Jul 11 '13 at 09:00
  • What do you want for `2013-03-31`, for February and April? – salathe Jul 11 '13 at 09:11

4 Answers4

1

Use it with PHP's strtotime():

echo date('Y-m-d', strtotime('+1 month')); //outputs 2013-08-11
echo date('Y-m-d', strtotime('-1 month')); //outputs 2013-06-11
  • @Nirjhor: feel free to accept this answer if you feel it helped. :-) –  Jul 11 '13 at 14:16
1
$date = new DateTime( "2013-07-11");
$date->modify("+1 month");
echo $date->format(‘l, F jS, Y’);
Expedito
  • 7,771
  • 5
  • 30
  • 43
0

Try this

$lst_month=mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$lst_month);
$next_month=mktime(0,0,0,date('m')+1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$next_month);
Hkachhia
  • 4,463
  • 6
  • 41
  • 76
0
$nextmonth=date("dmy",strtotime("+1 month"));
$lastmonth=date("dmy",strtotime("-1 month"));
Ravikant Upadhyay
  • 405
  • 1
  • 4
  • 12