Web Pro Source
Web Pro Source

Reputation: 181

php date() returning wrong month for specific months

I am extremely confused why date() is wrong on certain dates using the following code. The months listed below do the same as the code example showing the wrong month in number form.

echo $month = September 2019

$month_explode = explode(' ', $month);

$month_explode[0] = September

$month_explode[1] = 2019

echo date("m", strtotime($month_explode[0])); = 10 which would be October

The following months don't work...

Am I doing something wrong?

Upvotes: 1

Views: 364

Answers (1)

Web Pro Source
Web Pro Source

Reputation: 181

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]}"));

Upvotes: 6

Related Questions