I am putting together a weekly view calendar but I am having problems when getting to October 28th which is when the clocks go forward. The calendar skips a day
My code so far…
//get viewed date from form and add either a week to it or take a week away
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
//sets the current day as the first day of the week so no good
/*$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;*/
//Does't account for british summer time so days are out after 28th October
$display_week_ts = strtotime("Monday noon");
}
$week_start = new DateTime(date("Y-m-d", $display_week_ts));
for ($i = 0; $i < 7; $i++)
{
echo '<td class="day">';
$current_day_ts = $display_week_ts + ($i * 3600 *24);
$daily_date = date('d-m-Y', $current_day_ts);
$StartDate = date('d', $current_day_ts);
$MonthName = date('m', $current_day_ts);
$Year = date('Y', $current_day_ts);
echo $daily_date;
echo '</td>';
}
$week_start contains the value of the beginning of the week currently in view in the calendar. The first time it is opened the current week is displayed. If the next week button is pressed a week is added to the $week_start value. At present is saved in a hidden field in a table and posted back on submit. I have also tried storing $week_start as a TimeDate() object in a session
$week_start = new DateTime(date("Y-m-d", $display_week_ts));
$S_SESSION['week_start'] = $week_start;
But when I try to call the session back and use it move move a week forward
$week_start = $S_SESSION['week_start'];
$week_start->modify('+1 week');
I get the error ‘Warning: DateTime::modify() [datetime.modify]: The DateTime object has not been correctly initialized by its constructor’. After doing some digging I have found that DateTime doesn’t seem to support sessions until 5.3 and I am using 5.2.17
I would be really grateful if someone can help me workout a way to make the variable $week_start be the first day of the week represented by $display_week_ts in such a way that BST doesn’t cause a problem. I have been working on this solidly for 3 days now
use
date_default_timezone_set('UTC');or
$week_start->setTimezone(new DateTimeZone('UTC'));