homepage Welcome to WebmasterWorld Guest from 107.20.37.62
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
date + 4 days
jackdack




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

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

 

mm1220




msg:1279903
 11:03 pm on Dec 28, 2005 (gmt 0)

<?php

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

chrisjoha




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

You can also check out strtotime - strtotime('+4 days'); [no.php.net ]

mm1220




msg:1279905
 11:25 pm on Dec 28, 2005 (gmt 0)

Wow! That's what I love about php ... intuitive functions!

jackdack




msg:1279906
 3:13 am on Dec 29, 2005 (gmt 0)

looks great, i'll try thanks

madmac




msg:1279907
 3:21 pm on Dec 29, 2005 (gmt 0)

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 ;)

someone




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

I realized the problem with this:

<?php
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.

jackdack




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

it worked a treat, thanks

mm1220




msg:1279910
 2:12 pm on Jan 3, 2006 (gmt 0)

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:
mktime(0,0,0,0,date('d')+4)
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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved