6

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';
0

2 Answers 2

19

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;
2
  • @JohnAyers Updated my answer. Rename the date variables if you want to use the current date as well.
    – Josh
    Mar 3, 2012 at 23:25
  • appreciate the help! Didn't realize you could just added it in with date like that.
    – John Ayers
    Mar 3, 2012 at 23:30
7

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.

4
  • Seems like an exact copy of the previous answer which was posted 1 minute before yours...
    – Tim
    May 2, 2016 at 10:03
  • 1
    @Tim: 38 seconds, actually, in 2012 when new answers weren’t loaded with websockets. I guess what I’m saying is that I didn’t copy the answer and if you’re not suggesting that then I don’t know what the point of your comment was.
    – Ry-
    May 11, 2016 at 2:36
  • @Tim: If you’re referring to the use of $oneWeekLater in both answers (the non-bog-standard part), note that the other answerer edited it in relatively long afterwards and probably “copied” it from me (but I don’t care, because it’s a variable name).
    – Ry-
    May 11, 2016 at 2:42
  • Initially just looked like a copy paste. Please accept my apology
    – Tim
    May 11, 2016 at 4:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.