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
?>

2 Comments

  1. $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. Thanks for this :)


Post a Comment

*
*