tags:

views:

193

answers:

4

Many examples are about adding days to this day. But how to do it, if I have different starding day?

For example (Does not work):

$day='2010-01-23';

// add 7 days to the date above
$NewDate= Date('$day', strtotime("+7 days"));
echo $NewDate;

Example above does not work. How should I change the starding day by putting something else in the place of Date?

A: 

From php.com binupillai2003

<?php
/*
Add day/week/month to a particular date
@param1 yyyy-mm-dd
@param1 integer
by Binu V Pillai on 2009-12-17
*/

function addDate($date,$day)//add days
{
$sum = strtotime(date("Y-m-d", strtotime("$date")) . " +$day days");
$dateTo=date('Y-m-d',$sum);
return $dateTo;
}

?>
JonH
While "$date" may technically work, it's poor syntax, just say $date
TravisO
+4  A: 
$NewDate = strtotime('+7 days', strtotime($day));
antpaw
This will only output seconds since the unix epoch.
cballou
+1  A: 

For a very basic fix based on your code:

$day='2010-01-23';

// add 7 days to the date above
$NewDate = date('Y-m-d', strtotime($day . " +7 days"));
echo $NewDate;
cballou
A: 

Thanks for using my scripts.

Binu V Pillai