Bruce Gust
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?
$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?
You should be able to just do a +7 days in order to get the next week.
ASKER
Here's what I'm trying:
$week_one=date("m/d/Y", strtotime($week_start+7day s));
Not working.
Where am I blowing it?
$week_one=date("m/d/Y", strtotime($week_start+7day
Not working.
Where am I blowing it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would start by trying something like this.
$day = date('w'+7);
This should advance the day to 7 days in the future.
$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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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, 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
ASKER
Thanks, guys!
Here's where I landed:
$day = date('w');
$week_start = date('c', strtotime('-'.$day.' days'));
$this_week = date('c', strtotime($week_start));
Here's where I landed:
$day = date('w');
$week_start = date('c', strtotime('-'.$day.' days'));
$this_week = date('c', strtotime($week_start));