Hướng dẫn php date add month

I have a variable called $effectiveDate containing the date 2012-03-26.

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php date strtotime or ask your own question.
  • How can increase month in date in PHP?
  • How can I get plus date in PHP?
  • Which function can be used to change the date from current month to three months previous?
  • What does date [] do in PHP?

I am trying to add three months to this date and have been unsuccessful at it.

Here is what I have tried:

$effectiveDate = strtotime["+3 months", strtotime[$effectiveDate]];

and

$effectiveDate = strtotime[date["Y-m-d", strtotime[$effectiveDate]] . "+3 months"];

What am I doing wrong? Neither piece of code worked.

Pops

29.5k36 gold badges133 silver badges151 bronze badges

asked Mar 26, 2012 at 15:33

user979331user979331

9,42765 gold badges213 silver badges392 bronze badges

6

Change it to this will give you the expected format:

$effectiveDate = date['Y-m-d', strtotime["+3 months", strtotime[$effectiveDate]]];

answered Mar 26, 2012 at 15:41

5

This answer is not exactly to this question. But I will add this since this question still searchable for how to add/deduct period from date.

$date = new DateTime['now'];
$date->modify['+3 month']; // or you can use '-90 day' for deduct
$date = $date->format['Y-m-d h:i:s'];
echo $date;

answered Jun 5, 2018 at 10:11

SadeeSadee

2,66432 silver badges35 bronze badges

0

I assume by "didn't work" you mean that it's giving you a timestamp instead of the formatted date, because you were doing it correctly:

$effectiveDate = strtotime["+3 months", strtotime[$effectiveDate]]; // returns timestamp
echo date['Y-m-d',$effectiveDate]; // formatted version

answered Mar 26, 2012 at 15:41

NickNick

6,2282 gold badges28 silver badges46 bronze badges

You need to convert the date into a readable value. You may use strftime[] or date[].

Try this:

$effectiveDate = strtotime["+3 months", strtotime[$effectiveDate]];
$effectiveDate = strftime [ '%Y-%m-%d' , $effectiveDate ];
echo $effectiveDate;

This should work. I like using strftime better as it can be used for localization you might want to try it.

answered Mar 26, 2012 at 15:44

JohnnyQJohnnyQ

4,5476 gold badges44 silver badges64 bronze badges

Tchoupi's answer can be made a tad less verbose by concatenating the argument for strtotime[] as follows:

$effectiveDate = date['Y-m-d', strtotime[$effectiveDate . "+3 months"] ];

[This relies on magic implementation details, but you can always go have a look at them if you're rightly mistrustful.]

answered Dec 8, 2015 at 16:44

gleechgleech

3054 silver badges11 bronze badges

The following should work,Please Try this:

$effectiveDate = strtotime["+1 months", strtotime[date["y-m-d"]]];
echo $time = date["y/m/d", $effectiveDate];

mkl

86.4k14 gold badges124 silver badges246 bronze badges

answered Apr 4, 2017 at 7:29

Following should work

$d = strtotime["+1 months",strtotime["2015-05-25"]];
echo   date["Y-m-d",$d]; // This will print **2015-06-25** 

answered May 25, 2017 at 10:44

RickyRicky

1351 silver badge8 bronze badges

Add nth Days, months and years

$n = 2;
for [$i = 0; $i 

Chủ Đề