Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

date + 4 days

10:39 pm on Dec 28, 2005 (gmt 0)

5+ Year Member

I need a script that reads the current date on server and computes an additional 4 days to display a date.
e.g. today 29/12/05 + 4 days, the script would display 02/01/05 (or similar depending on formatting). Thanks
11:03 pm on Dec 28, 2005 (gmt 0)

10+ Year Member


echo 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).

11:17 pm on Dec 28, 2005 (gmt 0)

5+ Year Member

You can also check out strtotime - strtotime('+4 days'); [no.php.net ]
11:25 pm on Dec 28, 2005 (gmt 0)

10+ Year Member

Wow! That's what I love about php ... intuitive functions!
3:13 am on Dec 29, 2005 (gmt 0)

5+ Year Member

looks great, i'll try thanks
3:21 pm on Dec 29, 2005 (gmt 0)

10+ Year Member

yes strtotime() is great, though sometimes its calculations when using "Months" is off, but it will work fine in this case.

date('Y-m-d', strtotime('+4 days'))

Could not be easier... well it could but that is pretty darn easy ;)

6:14 pm on Dec 29, 2005 (gmt 0)

10+ Year Member

I realized the problem with this:

echo date("d/m/y", mktime(0, 0, 0, 0, (date('d') + 4)) );


is that the year doesn't change. That gives me 02/01/05, where the year should be 06.

11:07 am on Jan 2, 2006 (gmt 0)

5+ Year Member

it worked a treat, thanks
2:12 pm on Jan 3, 2006 (gmt 0)

10+ Year Member

In the interests of completeness let me explain what was wrong with my earlier posting and how it can be corrected.
I seeded mktime incorrectly. I was using:

which returns an incorrect timestamp.
mktime() takes in (hours, mins, secs, month, day, year)

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!


Featured Threads

Hot Threads This Week

Hot Threads This Month