Forum Moderators: coopster
<?phpecho date("d/m/y", mktime(0, 0, 0, 0, (date('d') + 4)) );
?>
Should do the trick!
A quick look at the php manual will give all the details as to how to format the output of the date.
The mktime function seeds the date function with a different timestamp (by default date() just looks to the server for one).
mktime(0,0,0,0,date('d')+4) It doesn't matter for the purposes of the original poster that we set hours, mins and seconds to 0. However in setting month to zero I was inadvertantly setting the month to December. 1 being January and 12 being December normally. The function also accepts other integer inputs and uses modular arthimetic to determine what month you want (cyclically 13 is January again and 14 February etc.) using this 0 is December.
Now, date('d') returns the day of the month. So when the error was first noticed by an earlier poster date('d') returned 28. 28 + 4 = 32 days added to the first of December gave the 1st of January as required. This however was purely conincidental because last month happened to be December. The error would have been apparent to us earlier if it had been July!
In any case the year was not updated. Seeing as we did not specify the year parameter passed into mktime(), it took the year from the system time (i.e. 2005). mktime() then looked at the number of days (32) and figured that 32 days onto the start of 2005 would not change the year so it didn't change the year to 2006.
To rectify this I now use date('z') which returns the "day of the year" and not "day of the month". It would have returned 361 instead of 28. Now confusingly this 'z' value for day of the year starts at 0 (unlike 'd'). So I need to add 1 to get the day-date. In this case 362 + 4 would trigger mktime to move the year forward as well as the month.
So, in conclusion the correct code is:
date("d/m/y", mktime(0, 0, 0, 1, date('z')+1+4); Although, mind you, I think I'll be using strtotime() from now on!