Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

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?
Avatar of Trent Smith
Trent Smith
Flag of United States of America image

You should be able to just do a +7 days in order to get the next week.
Avatar of Bruce Gust

ASKER

Here's what I'm trying:

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

Not working.

Where am I blowing it?
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would start by trying something like this.

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

This should advance the day to 7 days in the future.
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.
Thanks for correcting that for me Ray.  I appreciate the information.
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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

Thanks, guys!

Here's where I landed:

$day = date('w');
$week_start = date('c', strtotime('-'.$day.' days'));
$this_week = date('c', strtotime($week_start));