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.

Hướng dẫn php date add month

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 <= $n; $i++){
    $d = strtotime("$i days");
    $x = strtotime("$i month");
    $y = strtotime("$i year");
    echo "Dates : ".$dates = date('d M Y', "+$d days");
    echo "
"; echo "Months : ".$months = date('M Y', "+$x months"); echo '
'; echo "Years : ".$years = date('Y', "+$y years"); echo '
'; }

answered Aug 8, 2018 at 10:10

The following should work, but you may need to change the format:

echo date('l F jS, Y (m-d-Y)', strtotime('+3 months', strtotime($DateToAdjust)));

answered Mar 26, 2012 at 15:40

Brendon DuganBrendon Dugan

2,1107 gold badges32 silver badges63 bronze badges

public function getCurrentDate(){
    return date("Y-m-d H:i:s");
}

public function getNextDateAfterMonth($date1,$monthNumber){
   return date('Y-m-d H:i:s', strtotime("+".$monthNumber." months", strtotime($date1))); 
}

answered Sep 1 at 16:14

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?

Example 1: PHP Add Months to Date.

index.php. $date = "2021-11-01"; $newDate = date('Y-m-d', strtotime($date. ' + 3 months')); ... .

Output: 2022-02-01..

index.php. $newDate = date('Y-m-d', strtotime(' + 4 months')); echo $newDate; ?>.

Output: Read Also: How to Add Days to Date in PHP? 2022-03-01. i hope it can help you....

How can I get plus date in PHP?

The date_add() function adds some days, months, years, hours, minutes, and seconds to a date.

Which function can be used to change the date from current month to three months previous?

You can use the EDATE function to quickly add or subtract months from a date. The EDATE function requires two arguments: the start date and the number of months that you want to add or subtract. To subtract months, enter a negative number as the second argument. For example, =EDATE("9/15/19",-5) returns 4/15/19.

What does date () do in PHP?

The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. Note: These functions depend on the locale settings of your server.