Forum Moderators: coopster

Message Too Old, No Replies

How to add x number of days to a date

And return a valid date

         

surfgatinho

11:33 am on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,
I have been scratching my head over this one. I want to add a number of days to a date and end up with a date.

The only way I've thought of doing this so far is converting to a time stamp, adding the days (in milliseconds) and then converting back.
I'm sure there must be an easier way!

Suggestions appreciated.

Thanks

dreamcatcher

12:12 pm on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

You might want to check out the strtotime() [uk.php.net] function. It may be what you are looking for.

dc

brucec

2:46 pm on Apr 26, 2005 (gmt 0)

10+ Year Member



Also use the MKTIME function where you can do easy math through the epoch values. I use Epoch values to calculate the difference of days and they never fail.

To get more information on the epoch date math, search for the PHP MKTIME function in google and you will see good explanations of them.

It is very easy to use and usually can be done in 2 or 3 lines of PHP code.

surfgatinho

12:03 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the help. Here's what I did:

$startDate = mktime (0,0,0,$month,$day,$year);
$finishDate = $startDate + ($nights * 24 * 60 * 60);
$finishDate = date("Y-m-d",$finishDate);
$startDate = date("Y-m-d",$startDate);

Seems to work