John Ayers
John Ayers

Reputation: 519

Add one week to Date()

How do I add 1 week to a date function with separate date variables. The date needs to be separate so I can insert it into another form. If I cant do it as separate variables can i separate them afterwards?

So:

$year = date("Y");
$month = date("m");
$day = date("d");
echo 'Current Date' . $year . '.' . $month . '.' . $day;
echo 'Date 1 Week in the Future';

Upvotes: 6

Views: 19069

Answers (2)

Josh
Josh

Reputation: 8191

Use strtotime:

echo "Date 1 Week in the Future " . date('Y.m.d', strtotime('+1 Week'));
// Outputs: Date 1 Week in the Future 2012.03.10

To break up into separate variables (as per your comment):

$oneWeekLater = strtotime('+1 Week');

$year = date('Y', $oneWeekLater);
$month = date('m', $oneWeekLater);
$day = date('d', $oneWeekLater);

echo 'Date 1 Week in the Future' . $year . '.' . $month . '.' . $day;

Upvotes: 19

Ry-
Ry-

Reputation: 225005

PHP offers the convenient strtotime method for that:

$oneWeekLater = date('Y.m.d', strtotime('+1 week'));

To pass a custom date, use strtotime's second parameter.

Upvotes: 7

Related Questions