How can I add a week to this date?

I'm using this to get the starting date for this week:

$day = date('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days'));

...works like a charm.

What I want to do is add a week to "$week_start." I've been playing with some different scenarios and keep coming up short.

How do I do it?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Trent SmithCommented:
You should be able to just do a +7 days in order to get the next week.
Bruce GustPHP DeveloperAuthor Commented:
Here's what I'm trying:

$week_one=date("m/d/Y", strtotime($week_start+7days));

Not working.

Where am I blowing it?
Ray PaseurCommented:
http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html

Please see: http://iconoun.com/demo/temp_brucegust.php
<?php // demo/temp_brucegust.php

/**
 * http://www.experts-exchange.com/questions/28714666/How-can-I-add-a-week-to-this-date.html
 * http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html
 */
error_reporting(E_ALL);

$today = date('c');
$later = date('c', strtotime("$today + 1 week"));
var_dump($today, $later);

Open in new window

Discussion:
All internal representations of dates should be kept in the ISO-8601 format.  The article linked above explains all of this.  You can do date arithmetic with something like this or with mktime().  When you format dates for external display, that's when you use non-standard formats like m/d/y or similar, but we never use those during the programming and calculation parts -- we only use the ISO formats.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Trent SmithCommented:
I would start by trying something like this.

$day = date('w'+7);

This should advance the day to 7 days in the future.
Ray PaseurCommented:
PHP date('w') is documented here:
http://php.net/manual/en/function.date.php

It's the number of the day of the week, modulus 7.
Trent SmithCommented:
Thanks for correcting that for me Ray.  I appreciate the information.
Julian HansenCommented:
Using DateTime::add (http://php.net/manual/en/datetime.add.php) and DateInterval (http://php.net/manual/en/dateinterval.construct.php)
<?php

$x = new DateTime('2015-01-21');
$x->add(new DateInterval('P1W'));
print_r($x);

Open in new window

Output
DateTime Object
(
    [date] => 2015-01-28 00:00:00
    [timezone_type] => 3
    [timezone] => Africa/Johannesburg
)

Open in new window

Bruce GustPHP DeveloperAuthor Commented:
Guys! I appreciate the info.

Ray, here's your suggestion:

$today = date('c');
$later = date('c', strtotime("$today + 1 week"));

Perfect.

Here's my attempt to employ your wisdom in context:

$day = date('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days'));
//$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days'));

$later = date('m/d/Y', strtotime("$week_start + 7"));
echo $later;

Bleak. I get 12/31/1969

No doubt, I'm attempting a calculation on a value that's meant more for display purposes - and I'm making that statement based on your reference to mktime.

But, still, it seems like it should work. What am I missing?
Ray PaseurCommented:
Please see the man page for PHP date().  All of the formatting characters are documented there.

Here is the thought process to create the code...
1. What is the date that you want to start with?
2. Express that date in the ISO-8601 format with date('c')
3. Convert the ISO date to a Unix timestamp with strtotime()
4. Add some number of days, "x" to the ISO date with strtotime() using "+ x DAYS"
5. PHP strtotime() gives you a Unix timestamp.
6. Convert the new timestamp to the ISO-8601 format for storage in the database or other internal use
7. If you want a display-only value, convert the new timestamp to something like the m/d/y format, but do not use the display-only format in further calculations

HTH, ~Ray

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Confused as to what is wrong with the DateTime::add function with DateInterval - seems to do exactly what is required without converting dates to time stamps and back again?
Ray PaseurCommented:
Maybe this will shed a little more light on it?  I feel like we're not really understanding your problem yet.
<?php // demo/temp_brucegust.php

/**
 * http://www.experts-exchange.com/questions/28714666/How-can-I-add-a-week-to-this-date.html
 * http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html
 * http://php.net/manual/en/function.date.php
 */
error_reporting(E_ALL);


// SHOW HOW TO FIND THE FIRST AND LAST DAY OF THIS WEEK
$patrn = 'D Y-m-d';
$today = date($patrn);
$wdays = date('w');
$newer = date($patrn, strtotime($today . "- $wdays DAYS"));
$later = date($patrn, strtotime($newer . '+ 6 DAYS'));
var_dump($newer, $today, $later);

Open in new window

Bruce GustPHP DeveloperAuthor Commented:
Thanks, guys!

Here's where I landed:

$day = date('w');
$week_start = date('c', strtotime('-'.$day.' days'));
$this_week = date('c', strtotime($week_start));
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.