DateAdd Function on PHP

PHP Logo

If you are looking for a DateAdd function for PHP then see the sample code below.

The function accepts day only as the interval.


< ?php

/* --- DateAdd() function ---*/

function DateAdd($interval) {

 $curdate = getdate();
 $cday = $curdate['mday']+$interval;
 $cmonth = $curdate['mon'];
 $cyear = $curdate['year'];

  if ($cday &gt; 30){
    $cmonth = $cmonth + 1;
    $cday = $cday - 30;

  if ($cmonth == 13){
     $cyear = $cyear + 1;
     $cmonth = 1;
  }

 }

$ourDate = array($cyear,$cmonth,$cday);

  return $ourDate;

}
/* ---------- End of DateAdd() function ----------*/

/*Example*/

print DateAdd(10); //add 10 days to the current date

?>

Modify the code and play with the interval type to fit for your needs.

If you have corrections, comments or suggestions, Please feel free to post it here.

Permalink • Print • Comment

Trackback uri

http://seoroot.com/blog/computing/dateadd-function-on-php.html/trackback

Related Entries

Leave a Comment