Add days to a given date in PHP

When dealing with dates it is necessary to add or subtract a given number of days to a date! I use the following little function to do it in PHP.

function dateadd($day,$toadd) //input format: d/m/yyyy
{
$tmp = explode(“/”,$day);
$dadate = mktime(0,0,0,$tmp[1],$tmp[0]+($toadd),$tmp[2]);
return date(‘d/m/Y’,$dadate);
}

It is a simple function that split the inputed date into an array and create a date object with the number of additional days included in the creation. And then format the out put!

How to use:

<?php
$mydate =’24/12/2008′;
echo dateadd($mydate,30); //output 23/01/2009
?>

Advertisements

,

  1. #1 by Alex on July 8, 2009 - 8:42 pm

    $date = date(“Y-m-d”);// current date
    other method’s:

    $date = strtotime(date(“Y-m-d”, strtotime($date)) . ” +1 day”);
    $date = strtotime(date(“Y-m-d”, strtotime($date)) . ” +1 week”);
    $date = strtotime(date(“Y-m-d”, strtotime($date)) . ” +2 week”);
    $date = strtotime(date(“Y-m-d”, strtotime($date)) . ” +1 month”);
    $date = strtotime(date(“Y-m-d”, strtotime($date)) . ” +30 days”);

  2. #2 by Mohamad Fauzi on August 28, 2009 - 11:49 pm

    Thanks for this 🙂

  3. #3 by pj elbo on May 20, 2010 - 10:22 am

    wow thanks!
    very useful. i used it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: