Follow

Using “+/-1 Month” with strtotime() function in PHP

PHP’s date function strtotime is used to parse about any English textual date-time into a Unix time-stamp.

Syntax: int strtotime ( string $time [, int $now = time() ] );

Examples:

PHP Docs : http://php.net/manual/en/function.strtotime.php

We’ve been using it in one of our projects, to get the 28th day of the last month.

What we were expecting:

Assume that today’s date is: 2014-12-31 and we wanted last month’s 28th.

Our code –

What do you think the $date will have? 2014-11-28 right? Wrong! It has 2014-12-28

Let’s look at another example –

Assume that today’s date is 2015-01-31, and we want to get the next month’s 28th.

Our code –

[code language=”php”]<?php $date = <strong>date(‘Y-m-28’,  strtotime(“+1 month”))?> [/code]

The $date will have 2015-03-28, instead of 2015-02-28 as expected.

Scratching your head? We were too, but delving a little deeper into the implementation made things clearer.

What’s happening?

If a month has 31 days and you are on the last day of that month, the strtotime(“-1 month”)    returns the 1st day of  that month.

If you are on last day of January, the strtotime(“+1 month”) returns 3rd of March(not a leap year) / 2nd March(leap year).

Why it’s not a bug:

The current behavior is correct (in it’s own way). Let’s understand the process with an example:

  • Assume today’s date is 2015-01-31, and you want to add “+1 month” to it.
  • Actually +1 month increases the month number (originally 1) by one. This makes the date 2015-02-31.
  • But the 2nd month (February) only has 28 days in 2015, so PHP auto-corrects this by just continuing to count days from February 1st. You then end up at March 3rd.You can see this here: http://ideone.com/HNalGr
  • A similar thing happens, when you run “-1 month”. You can see this here: http://Ideone.Com/Yvm5p1

How to get what you want:

If you are using PHP 5.3 and above, to obtain the desired behavior, you can use the relative time stanza – first day of. This stanza can be used in combination with next month, fifth month or +9 months to go to the first day of the specified month. Instead of +1 month from what you’re doing, you can use this code to get the first day of next month like this:

How it works(the above solution):

  1. The new DateTime creates a date object of the date that we given as parameter or today date.
  2. Now, we are modifying the date to “first day of next month” (or “first day of last month”).
  3. Then we are echoing the date in the format that we want (i.e., yyyy-mm-28) by using format functionality of date

You can see this example(with execution) here:

  1. http://ideone.com/jw25JV
  1. http://ideone.com/pStgEK

A universal solution to get what you want:

If you are using PHP 5.3 or above, we recommend going with the above solution. Otherwise, you can use PHP’s mktime function to get what you need.

PHP Docs : http://php.net/manual/en/function.mktime.php

You can see this example(with execution) here:

http://ideone.com/tgP1OT

The following are some more examples to understand better that how to use strtotime.

PHP fiddle link: http://ideone.com/VRMCyW

 

Are you interested? follow us and get notified of new posts

3 thoughts on “Using “+/-1 Month” with strtotime() function in PHP

  1. Ale
    at 2:50 pm

    echo Date(“Y-m-d”, strtotime(“2013-01-31 +2 Month”)); // 2013-03-31

    e assim ?

    echo Date(“Y-m-d”, strtotime(“2013-01-31 +1 Month”)); //?

    1. Sampath Kumar G
      at 10:21 am

      Hello Ale, Could you give more details?

      And If you do
      echo Date(“Y-m-d”, strtotime(“2013-01-31 +1 Month”));

      It echos –> 2013-03-03

  2. robert mwanri
    at 4:39 pm

    <?php $date = date(‘Y-m-28’, strtotime(“+1 month”))?>
    The $date will have 2015-03-28, instead of 2015-02-28 as expected

    its ridiculous to call this not a bug, i think it is a bug if not on program then on those programmers heads, those justifications are childish, a programmer should consider all factors, should make a program that think like humans not like machines others wise will be those programs should be used by robots only may be they will understand.

Leave A Reply

two + 19 =