Bruce Gust
asked on
I'm off by one day. How can I fix it?
Below is a screenshot of a calendar I'm building. Check it out:
Every column is labeled according to the date that particular week begins, which is a Sunday. At least, that's the idea.
Here's what I'm using to craft those dates:
First, I look to see if the week_number is embedded in the URL. If not, we calculate the beginning date of this week according to today's date:
Now I either add or subtract weeks to get my column headings. Like this:
Thing is, I'm off by one day. If you look at my screenshot, instead of the day of this week being 9/13/2015, it's 9/14/2015. How can I fix that?
Every column is labeled according to the date that particular week begins, which is a Sunday. At least, that's the idea.
Here's what I'm using to craft those dates:
First, I look to see if the week_number is embedded in the URL. If not, we calculate the beginning date of this week according to today's date:
if(!isset($_GET['week_number']))
{
$day = date('w');
$week_start = date('c', strtotime('-'.$day.' days'));
}
else
{
$date = date_create();
date_isodate_set($date, $this_year, $_GET['week_number']);
$week_start= date_format($date, 'Y-m-d');
}
Now I either add or subtract weeks to get my column headings. Like this:
$this_week = date('c', strtotime($week_start));
$week_one = date('c', strtotime("$this_week - 6 weeks"));
$week_two = date('c', strtotime("$this_week - 5 weeks"));
$week_three = date('c', strtotime("$this_week - 4 weeks"));
$week_four = date('c', strtotime("$this_week - 3 weeks"));
$week_five = date('c', strtotime("$this_week - 2 weeks"));
$week_six = date('c', strtotime("$this_week - 1 week"));
//$week_seven is documented as $week_start
$week_eight = date('c', strtotime("$this_week + 1 week"));
$week_nine = date('c', strtotime("$this_week + 2 weeks"));
$week_ten = date('c', strtotime("$this_week + 3 weeks"));
$week_eleven = date('c', strtotime("$this_week + 4 weeks"));
$week_twelve = date('c', strtotime("$this_week + 5 weeks"));
$week_thirteen = date('c', strtotime("$this_week + 6 weeks"));
Thing is, I'm off by one day. If you look at my screenshot, instead of the day of this week being 9/13/2015, it's 9/14/2015. How can I fix that?
Here's how I might do it... Just get all of the Sundays into an array (or all of the days of the year - it's still a short array) then you can reduce the amount of code and get accurate information from the PHP date/time algorithms. Be careful about your timezone settings, too. Some of the PHP algorithms will assume you're in UTC unless you tell them otherwise with a timezone setting. I'm several hours away from UTC and this can cause the appearance of the wrong date during some parts of the day.
This will ensure that you're working with good data. And with OOP notation you can get of all the fiddly quotation marks!
<?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://www.experts-exchange.com/articles/20920/Handling-Date-and-Time-in-PHP-and-MySQL-OOP-Version.html
* http://php.net/manual/en/datetime.setisodate.php
* http://php.net/manual/en/datetime.format.php
* http://php.net/manual/en/function.date.php
*/
error_reporting(E_ALL);
echo '<pre>';
// WE WILL USE THE OOP DATE OBJECT
$date = new DateTime();
// WE SILL CREATE OUR OWN CLASS TO HOLD THE PROPERTIES WE NEED FOR EACH DATE
Class MyDate
{
// STORES THE PROPERTIES FROM THE DATETIME->FORMAT()
public $Y, $m, $d, $w; // YEAR see PHP date()
public $D, $l, $z; // DAY D == abbrev, ell == full day name, z == index number
public $M, $F, $t; // MONTH M == abbrev, F == full month name, t == number of days
public function __Construct($datetime)
{
$this->Y = $datetime->format('Y');
$this->m = $datetime->format('m');
$this->d = $datetime->format('d');
$this->w = $datetime->format('w');
$this->D = $datetime->format('D');
$this->l = $datetime->format('l');
$this->z = $datetime->format('z');
$this->M = $datetime->format('M');
$this->F = $datetime->format('F');
$this->t = $datetime->format('t');
}
}
$array = [];
$alpha = new DateTime('2015-01-01');
$omega = new DateTime('2015-12-31');
$intvl = new DateInterval('P1D');
while ($alpha <= $omega)
{
$array[] = new MyDate($alpha);
$alpha->add($intvl);
}
print_r($array);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's the ticket, Ray!
Thank you, sir!
Thank you, sir!