Friday, 8 July 2011

Adding days to the date in PHP

Had to do this one for St.Moritz hotel in Polzeath . Basicall needed to add a day to the availabilty search, so booking can't be made on the sameday.

the date was being called by this code

$date_now = date('Y-m-d');

here's the function I wrote

function add_days($date,$days){ // have remade to use mktime // date to come into function in Y-m-d

$date_array = explode('-', $date);
list($year, $month, $day) = $date_array;
$new_day = $day+ $days;
$date_add_days = date('Y-m-d', mktime(12, 0, 0, $month, $new_day, $year));
return $date_add_days;


and called it up with

$date_now = add_days($date_now ,'1');

immediatley after finding out the value to $date_now

with this by changing the 1 you can add as many days as you'd like.

No comments: