adding days to a date

liunx

Guest
Using php how would i add a variable number of days on to the current date (displaying dd/mm/yy)// assuming you already have a $date variable with a date in it
$date_array = explode(".", $date);

$date_array[0] = $date_array[0] + $number_of_days;

// you'll need to test if the day goes over the end of the month, and make the following changes if it does

if ($date_array[0] > $length_of_month)
{
$date_array[0] = $date_array[0] - $length_of_month;
$date_array[1] = $date_array[1] + 1;
}

// keep testing the day value until it is within a month

// now you'll need to test if the month goes over the end of the year

if ($date_array[1] > 12)
{
$date_array[1] = $date_array[1] - 12;
$date_array[2] = $date_array[2] + 1;
}

// again repeat until the month is 12 or less

$new_date = implode(".", $date_array);


HKuhhh...couldnt you just do:
$date = date("d/m/y", time()+(86400 * $num_of_days));
so you dont have to do all that checking and stuff, and have php do it for you...didn't know if you could or not.

while i know programming (C++), I am not as proficient in PHP.or

echo strtotime ("+1 day"), "\n";
echo strtotime ("+1 week"), "\n";
echo strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n";
 
Back
Top