35

I have two columns in my db: start_date and end_date, which are both DATE types. My code is updating the dates as follows:

$today_date = date("Y-m-d");
$end_date = date("Y-m-d"); // date +1 month ??

$sql1 = "UPDATE `users` SET `start_date` = '".$today_date."', `end_date` = '".$end_date."'  WHERE `users`.`id` ='".$id."' LIMIT 1 ;";

What is the best way to make $end_date equal to $start_date + one month? For example, 2000-10-01 would become 2000-11-01.

2

14 Answers 14

91

You can use PHP's strtotime() function:

// One month from today
$date = date('Y-m-d', strtotime('+1 month'));

// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));

Just note that +1 month is not always calculated intuitively. It appears to always add the number of days that exist in the current month.

Current Date  | +1 month
-----------------------------------------------------
2015-01-01    | 2015-02-01   (+31 days)
2015-01-15    | 2015-02-15   (+31 days)
2015-01-30    | 2015-03-02   (+31 days, skips Feb)
2015-01-31    | 2015-03-03   (+31 days, skips Feb)
2015-02-15    | 2015-03-15   (+28 days)
2015-03-31    | 2015-05-01   (+31 days, skips April)
2015-12-31    | 2016-01-31   (+31 days)

Some other date/time intervals that you can use:

$date = date('Y-m-d'); // Initial date string to use in calculation

$date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+2 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 month', strtotime($date)));
$date = date('Y-m-d', strtotime('+30 days', strtotime($date)));
7
  • Thanks for this - I had no idea PHP was clever enough to turn +1 month into a time! I take it PHP works out +1 month from the current time (obviously)?
    – Bojangles
    Nov 30, 2010 at 21:47
  • 3
    That is correct. Have a play, it also accepts things like "next thursday", "last tuesday" and "+1 week 3 days 2 hours 4 seconds"
    – Gazler
    Nov 30, 2010 at 21:48
  • 7
    @JamWaffles and @SzamDev, you should check that you're happy with the result when the current date is 31 January. Date arithmetic with months sometimes gives unexpected results.
    – Don Kirkby
    Nov 30, 2010 at 22:06
  • 4
    This will not work if the date is 30 January 2014 or 31 January 2014
    – DXB-DEV
    Sep 23, 2014 at 11:49
  • I edited the question to make it clearer where dates can go a bit haywire. Thanks @DonKirkby.
    – Simon E.
    Aug 6, 2015 at 5:31
25

The accepted answer works only if you want exactly 31 days later. That means if you are using the date "2013-05-31" that you expect to not be in June which is not what I wanted.

If you want to have the next month, I suggest you to use the current year and month but keep using the 1st.

$date = date("Y-m-01");
$newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;

This way, you will be able to get the month and year of the next month without having a month skipped.

4
  • Yes, thanks @Patrick, I made an edit to the accepted answer to note the confusing way it does calculations sometimes.
    – Simon E.
    Aug 6, 2015 at 5:39
  • Thank you. This is very clever.
    – VVV
    Jan 30, 2017 at 23:40
  • $nextm = date('m', strtotime('+1 month', strtotime(date('Y-m-01'))));
    – drooh
    Jan 31, 2020 at 21:37
  • @Patrick Hi, I was looking for the same solution. But there is a trick with my problem. I want to count every single month between specific dates. For example, if there is a date 2022-12-23 in the database now I want to store 2022-01-23 in the variable which can be done with the above code but if the 2022-01-23 is already passed in real life then I want to store the next month for example 2022-02-23 how can I do that, please?
    – CODAR747
    May 7, 2022 at 10:13
18

You do not actually need PHP functions to achieve this. You can already do simple date manipulations directly in SQL, for example:

$sql1 = "
    UPDATE `users` SET 
    `start_date` = CURDATE(), 
    `end_date` = DATE_ADD(CURDATE(), INTERVAL 1 MONTH)  
    WHERE `users`.`id` = '" . $id . "';
";

Refer to http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_addtime

1
  • I was going to post an answer similar to this, but I don't quite remember how to do it - thanks for posting the solution. +1
    – Bojangles
    Dec 1, 2010 at 8:44
17

If you want a specific date in next month, you can do this:

// Calculate the timestamp
$expire = strtotime('first day of +1 month');

// Format the timestamp as a string
echo date('m/d/Y', $expire);

Note that this actually works more reliably where +1 month can be confusing. For example...

Current Day   | first day of +1 month     | +1 month
---------------------------------------------------------------------------
2015-01-01    | 2015-02-01                | 2015-02-01
2015-01-30    | 2015-02-01                | 2015-03-02  (skips Feb)
2015-01-31    | 2015-02-01                | 2015-03-03  (skips Feb)
2015-03-31    | 2015-04-01                | 2015-05-01  (skips April)
2015-12-31    | 2016-01-01                | 2016-01-31
2
  • 1
    This is such an important answer. I found it strange that my code on the 31st of May was coming up with July from a +1 month command. This would explain it! May 31, 2016 at 13:06
  • This is what actually I needs, the important think to know! Thanks Mar 30, 2022 at 2:42
2

Adding my solution here, as this is the thread that comes in google search. This is to get the next date of month, fixing any skips, keeping the next date within next month.

PHP adds current months total number of days to current date, if you do +1 month for example.

So applying +1 month to 30-01-2016 will return 02-03-2016. (Adding 31 days)

For my case, I needed to get 28-02-2016, so as to keep it within next month. In such cases you can use the solution below.

This code will identify if the given date's day is greater than next month's total days. If so It will subtract the days smartly and return the date within the month range.

Do note the return value is in timestamp format.

function getExactDateAfterMonths($timestamp, $months){
    $day_current_date            = date('d', $timestamp);
    $first_date_of_current_month = date('01-m-Y', $timestamp);
    // 't' gives last day of month, which is equal to no of days
    $days_in_next_month          = date('t',  strtotime("+".$months." months", strtotime($first_date_of_current_month)));

    $days_to_substract = 0;
    if($day_current_date > $days_in_next_month)
         $days_to_substract = $day_current_date - $days_in_next_month;

    $php_date_after_months   = strtotime("+".$months." months", $timestamp);
    $exact_date_after_months = strtotime("-".$days_to_substract." days", $php_date_after_months);

    return $exact_date_after_months;
}

getExactDateAfterMonths(strtotime('30-01-2016'), 1);
// $php_date_after_months   => 02-03-2016
// $exact_date_after_months => 28-02-2016
1

You can use strtotime() as in Gazler's example, which is great for this case.

If you need more complicated control use mktime().

$end_date = mktime(date("H"), date("i"), date("s"), date("n") + 1, date("j"), date("Y"));
2
  • Won't this be rather slow? All those date() functions?
    – Bojangles
    Dec 1, 2010 at 8:45
  • Those are the default values for the function, there for illustration. You could use a single date(), split it and throw it in. Which will return fastest is hard to say. strtotime(), the alternative, is far from fast as one might expect.
    – Orbling
    Dec 1, 2010 at 9:24
1
date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate)))
1
  • 1
    While this code may answer the question, it would be better to explain how it solves the problem without introducing others and why to use it. Code-only answers are not useful in the long run.
    – JAL
    Nov 19, 2015 at 15:18
1
date_trunc('month', now()) + interval '1 month' 

date_trunc('month', now()) returns start date of month and + interval '1 month' add a month to date.

0

This function returns any correct number of months positively or negatively. Found in the comment section here:

function addMonthsToTime($numMonths = 1, $timeStamp = null){
    $timeStamp === null and $timeStamp = time();//Default to the present
    $newMonthNumDays =  date('d',strtotime('last day of '.$numMonths.' months', $timeStamp));//Number of days in the new month
    $currentDayOfMonth = date('d',$timeStamp);

    if($currentDayOfMonth > $newMonthNumDays){
      $newTimeStamp = strtotime('-'.($currentDayOfMonth - $newMonthNumDays).' days '.$numMonths.' months', $timeStamp);
    } else {
    $newTimeStamp = strtotime($numMonths.' months', $timeStamp);
    }

    return $newTimeStamp;
}
0

01-Feb-2014

$date = mktime( 0, 0, 0, 2, 1, 2014 );

echo strftime( '%d %B %Y', strtotime( '+1 month', $date ) );
0

I think this is similar to kouton's answer, but this one just takes in a timeStamp and returns a timestamp SOMEWHERE in the next month. You could use date("m", $nextMonthDateN) from there.

function nextMonthTimeStamp($curDateN){ 
   $nextMonthDateN = $curDateN;
   while( date("m", $nextMonthDateN) == date("m", $curDateN) ){
      $nextMonthDateN += 60*60*24*27;
   }
   return $nextMonthDateN; //or return date("m", $nextMonthDateN);
}
0

I know - sort of late. But I was working at the same problem. If a client buys a month of service, he/she expects to end it a month later. Here's how I solved it:

    $now = time(); 
    $day = date('j',$now);
    $year = date('o',$now);
    $month = date('n',$now);
    $hour = date('G');
    $minute = date('i');
    $month += $count;
    if ($month > 12)        {
            $month -= 12;
            $year++; 
            }
    $work = strtotime($year . "-" . $month . "-01");
    $avail = date('t',$work);
    if ($day > $avail)
            $day = $avail;
    $stamp = strtotime($year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute);

This will calculate the exact day n*count months from now (where count <= 12). If the service started March 31, 2019 and runs for 11 months, it will end on Feb 29, 2020. If it runs for just one month, the end date is Apr 30, 2019.

0

$nextm = date('m', strtotime('+1 month', strtotime(date('Y-m-01'))));

0

one way of doing this is- first getting last date of current month and then adding 1 day to it, to get next month's first date. This will prevent us from unintentional skipping of months which occurs while using '+1 month'.

$today_date = date("Y-m-d"); 
$last_date_of_month=date('Y-m-t',strtotime($today_date);//get last date of current month
$last_day_of_month_proper =strtotime($last_day_of_month);
$new_date=date('Y-m-d', strtotime('+1 day',$last_day_of_month_proper)); 
echo $new_date;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.