Web Hosting Talk







View Full Version : Strange Date Issue, Please Help!


pphillips
01-31-2006, 01:51 PM
echo "next month: " . date("F", strtotime(date("Y-m-d", strtotime("January 31 2006")) . " +1 month"));

This outputs March but since it is January 31, it should echo February. It worked fine for the past several months until now. Any ideas? I'm stumped on this one. Seems to act the same with PHP 4.3.9 and PHP 5.0.4.

There is probably a better way to write this and that may be my problem. Any help would be greatly appreciated, thanks!

MODERATOR: Sorry for the double post. The site did not load after trying to post this and I clicked refresh, feel free to delete one.

Korvan
01-31-2006, 03:17 PM
date("F", strtotime("+1 month", strtotime("January 31 2006")));

Will do what you want. That should calculate +1 month from january 31 2006.

The second arguement in strtotime while optional must also be a timestamp. if it is unspecified it defaults to time();

pphillips
01-31-2006, 05:26 PM
That is a much simpler way to do it, though, your code still echos March. After looking around I was unable to avoid this using strtotime, it appears to be a bug in PHP. Rather than adding 1 month, it appears to add 31 days. I had to use the following instead:

date("M", mktime(0,0,0,date("n")+1,1,date("Y")));

Any ideas?

Burhan
02-01-2006, 02:52 AM
Don't you have another thread with the same topic? Don't start duplicate threads.

Korvan
02-01-2006, 02:27 PM
fyre, read his statement after moderator: he would like the other thread to be removed.

yea wshost, the reason is that it starts its calculation from midnight (as explained by fyre in the other thread), you could fix it by setting it to noon and it should output feb. Though mktime is also a good solution and should work just fine.