PHP Date() and Strtotime() Return Wrong Months on 31St

PHP date() and strtotime() return wrong months on 31st

As mentioned within the documentation, you should pass the date for the first day of the current month as the second parameter to the strtotime() function:

$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
echo date('m/Y',strtotime('+0 month', $base));
echo date('m/Y',strtotime('+1 month', $base));
echo date('m/Y',strtotime('+2 month', $base));

See that it works: http://ideone.com/eXis9

01/2012

02/2012

03/2012

Will PHP date() and strtotime() return the wrong time with week str for the month of Feb.?

Try this. (PHP object-oriented format.)

<?php

$d = new DateTime;
$interval = new DateInterval('P1W');
for ($i = 25; $i < 60; $i++)
{
$d->setDate(2014, 2, $i);
echo $d->format('Y-m-d'), " ";

$d->sub($interval);
echo $d->format('Y-m-d'), PHP_EOL;
}

There's nothing ambiguous about the number of days you mean when you say 1 week. When I say 1 week, I always means 7 days. But the term 1 month is fuzzy. I might mean 28, 29, 30, or 31 days. (Change "P1W" to "P1M" in the code above, and run it again. Look at the last four lines in the result.

Also note that the php documentation for strtotime() says, "Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2."

Also see this warning: Example #3 Beware when subtracting months

php date() returning wrong month for specific months

It just hit me as I was reviewing my question. I am posting this so that maybe this will help someone else dealing with the same issue.

IF you just use the month in strtotime it will auto append the current "day of month" and "year" in numeric form. Because today is the 31st, it tries to calculate September 31st which doesn't exist. So it auto jumps to the next month. Same for all of the months listed. If I change the code to the following everything works because every month has a "1st" of the month.

echo date("m", strtotime("first day of {$month_explode[0]}"));

Why does PHP date('m', strtotime('-1 months')) not work correctly for today? 07/31

Here's a cleaner test case that doesn't expire:

<?php
$origin = mktime(18, 0, 0, 7, 31, 2015);
var_dump( date('r', $origin), date('r', strtotime('-1 months', $origin)) );
string(31) "Fri, 31 Jul 2015 18:00:00 +0200" 
string(31) "Wed, 01 Jul 2015 18:00:00 +0200"

I'm pretty sure it's a documentation issue, because the manual clearly states this (emphasis mine):

Relative month values are calculated based on the length of months
that they pass through. An example would be "+2 month 2011-11-30",
which would produce "2012-01-30". This is due to November being 30
days in length, and December being 31 days in length, producing a
total of 61 days.

... and it's wrong.

PHP bug tracker has tons of dupes about this. They're all closed as not a bug. Here's a relevant comment from 2009 that explains it:

I agree that this is an annoying behaviour.

Also, the implementation is problematic. Basically if you use '+1
month' it takes the month number, adds 1 and parses result as a new
date
.

If you use '+1 month' on the first of the month, it sets the date to
the next first of the month.

This behaviour gives the impression, that php considers the length of
a month, which is not true.

But if you use '+1 month' on the last day of a month, the result is
unexpected
as 2009-05-31 becomes 2009-06-31 which is an invalid date
and then interpreted as 2009-07-01.

This should at least be mentioned in the documentation.

strtotime function giving wrong output, How to solve?

date("M Y", strtotime("-1 months"));

This is true but today is 30th March that's why you are not getting exact result which you want. So, we should use 1st day of month and then apply substraction.

What is causing strtotime to return wrong relative weekday?

This is a bug:

https://bugs.php.net/bug.php?id=63521

Just subtract 2 days when it returns a Sunday.

strtotime('-1 month') returning wrong date if month have 31 days

As you can see working with the end of the month can be problematic because of how PHP works with dates. Your best bet is to go back to the beginning of the month, do your date math (i.e. go backwards in time), and then go to the date you want. That way you can check to see if the current day is greater than the number of days in month. If so, use the last day of the month instead.

function getMonthsAgo(int $n): string {
$date = new DateTime();
$day = $date->format('j');
$date->modify('first day of this month')->modify('-' . $n . ' months');
if ($day > $date->format('t')) {
$day = $date->format('t');
}
$date->setDate($date->format('Y'), $date->format('m'), $day);
return $date->format('Y-m-d');
}

// Dates Formated
$date_now = date('Y-m-d');
$date_month1 = getMonthsAgo(1);
$date_month2 = getMonthsAgo(2);
$date_month3 = getMonthsAgo(3);

//Output
echo $date_now;
echo $date_month1;
echo $date_month2;
echo $date_month3;

Output:

2020-04-30
2020-03-30
2020-02-29
2020-01-30

PHP date returning wrong value

Instead of using the old date/time functions that mess things up because they implicitly involve your local time zone, use DateTime:

$date = new DateTime('Tue May 01 00:00:00 +1000 2012');
echo $date->format('F Y');

This will also work corrrectly for any date, regardless of the timezone (UTC+10 hours or anything else).

PHP Strtotime -1month -2month

It might be related to bug #44073

You could try with something like this :

echo date("M", strtotime("-3 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", strtotime("-2 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", strtotime("-1 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", time()) . "\n";

(Solution found in the comments section of strtotime ; direct link)

And the output :

Apr
May
Jun
Jul

Kind of "cheating" with the date format and month's name and all that...



Related Topics



Leave a reply



Submit