php strtotime last day last month function produces 2 months ago

According to server:

 $ date
 Wed Mar  1 01:46:06 EST 2017

php.ini

 date.timezone = America/New_York

When I do this I get an odd result:

 $month = date('M',strtotime('last day last month'));
 $sel = "SELECT records where month='".$month."'";
 echo $sel;

The result is:

  SELECT records where month='Jan';

any ideas why not Feb?

When using "next month", "last month", "+1 month", "-1 month" or any combination of +/-X months.

It will give non-intuitive results on Jan 30th and 31st.

As described at : http://derickrethans.nl/obtaining-the-next-month-in-php.html

<?php
    $d = new DateTime( '2010-01-31' );
    $d->modify( 'next month' );
    echo $d->format( 'F' ), "\n";
?>

In the above, using "next month" on January 31 will output "March" even though you might want it to output "February".

("+1 month" will give the same result. "last month", "-1 month" are similarly affected, but the results would be seen at beginning of March.)

The way to get what people would generally be looking for when they say "next month" even on Jan 30 and Jan 31 is to use "first day of next month":

<?php
    $d = new DateTime( '2010-01-08' );
    $d->modify( 'first day of next month' );
    echo $d->format( 'F' ), "\n";
?>


<?php
    $d = new DateTime( '2010-01-08' );
    $d->modify( 'first day of +1 month' );
    echo $d->format( 'F' ), "\n";
?>

php strtotime last day last month function produces 2 months ago, According to server: $ date Wed Mar 1 01:46:06 EST 2017 php.ini date.timezone = America/New_York When I do this I get an odd result: $month = date('M'  In the example above, we have the date string “2014-02-04”, which is a YYYY-MM-DD date for the 4th of February, 2014. To get the last date of February, 2014, we simply convert the date to a UNIX timestamp using PHP’s strtotime function; before using the resulting timestamp as the second parameter in our date function. If you run the PHP code above, you’ll see that the result is: “2014-02-28”.

Can you please try previous month instead of last month

  $month = date('M',strtotime('last day of previous month')); 

php strtotime last day last month function produces 2 months ago, php strtotime last day last month function produces 2 months ago. According to server: $ date Wed Mar 1 01:46:06 EST 2017. php.ini date.timezone = America/  Hi, I would like to have a date, which would be the last day of the 2 past months, e.g. : scripts runs at 06/02/2009 need: 20090131 and 20081231

use code if you want last month

$month = date('M',strtotime('last month'));

$sel = "SELECT records where month='".$month."'";

echo $sel;

Result :

SELECT records where month='Feb'

Relative Formats - Manual, $month = date('M',strtotime('last day of previous month')); strtotime - Manual, 'last day of', Sets the day to the last day of the current month. "2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months ago 5 days", "2 days ago". DateTime::modify - Manual, Previous to PHP 5.1. 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).

// previous month
echo date("F t, Y", strtotime("last month"));

// two months ago
echo date("F t, Y", strtotime("last month -1 month"));

strtotime - Manual, 'last day of', Sets the day to the last day of the current month. "2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months ago 5 days", "2 days ago". Version Description; 5.3.0: Prior to PHP 5.3.0, relative time formats supplied to the time argument of strtotime() such as this week, previous week, last week, and next week were interpreted to mean a 7 day period relative to the current date/time, rather than a week period of Monday through Sunday.

DateTime::modify - Manual, Previous to PHP 5.1.0, this function would return -1 on failure. last week, and next week were interpreted to mean a 7 day period relative to the current date/​time echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP: 2009-03-​31 The following code will produce difference output in 32 and 64 bit environments. Day of the month without leading zeros: 1 to 31: l (lowercase 'L') A full textual representation of the day of the week: Sunday through Saturday: N: ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday) S: English ordinal suffix for the day of the month, 2 characters: st, nd, rd or th.

Using "+/-1 Month" with strtotime() function in PHP, The function modifies this object. Valid formats are explained in Date and Time Formats. Example #2 Beware when adding or subtracting months strtotime() - Parse about any English textual datetime description into a Unix timestamp elseif($aDate == $dateB->modify('last day of this month')){ produces this: 'last day of' Sets the day to the last day of the current month. This phrase is best used together with a month name following it. "last day of next month" ordinal space dayname space 'of' Calculates the x-th week day of the current month. "first sat of July 2008" 'last' space dayname space 'of' Calculates the last week day of the current month.

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems, PHP's date function strtotime is used to parse about any English textual If you are on last day of January, the strtotime(“+1 month”) returns 3rd of This stanza can be used in combination with next month, fifth month or +9 months to go strtotime("2013-01-31 +2 Month")); // 2013-03-31 echo Date("Y-m-d",  Description: ----- strtotime("+1 Month") produces the wrong result on the 31st day of the month. on January 31, date("m",strtotime("+1 Month")) will return 03 instead of 02 even if the documentation were to state that this is acceptable, it basically renders the function useless, since you'd have to always add a bunch of extra code to find out

Comments
  • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.