I’m losing hair over this :hair: I have the following function. The parameter accepts a date, then the function generates a table with the days of the week for that date. But when you submit the present week, it displays two Sundays, instead of one. I’m not too experienced handling dates and times. Can anyone please help me?
function createWeek($enterDate)
{
echo '<h1>'.date('l', strtotime($enterDate)).'</h1>';
if(date('l', strtotime($enterDate))=='Sunday')
{
echo '<h2>its a sunday</h2>';
$startDate = date('r',strtotime($enterDate));
$endDate = date('r',strtotime('next Saturday '.$enterDate));
}
elseif(date('l', strtotime($enterDate))=='Saturday')
{
echo '<h2>its a saturday</h2>';
$startDate = date('r',strtotime('last Sunday '.$enterDate));
$endDate = date('r',strtotime('this Saturday '.$enterDate));
}
else
{
echo '<h2>not saturday nor sunday</h2>';
$startDate = date('r',strtotime('last Sunday '.$enterDate));
$endDate = date('r',strtotime('next Saturday '.$enterDate));
}
// echo '<pre>';
// print_r(getdate(strtotime($enterDate)));
// echo '</pre>';
// echo 'Date: '.date('l F j, Y', strtotime($enterDate)).'<br />';
// echo date('W', strtotime($enterDate)).'th week<br />';
// echo 'Start day: '.$startDate;
// echo '<br />';
// echo 'End day: '.$endDate.'<br />';
$weekDaysLabel = '<tr class="daysOfWeek"><td>'.date('Y', strtotime($startDate)).'</td>';
$daysTime = '<tr>';
// cells for each day of the week
for($i=0;$i<7;$i++)
{
$weekDaysLabel .= '<td>'.date('l, M j', strtotime('+'.$i.' day '.$startDate)).' | +'.$i.' day '.$startDate.'</td>';
}
// tr's, rows for each time
$time = array('6 AM', '7 AM', '8 AM', '9 AM', '10 AM', '11 AM', 'NOON', '1 PM', '2 PM', '3 PM', '4 PM', '5 PM', '6 PM', '7 PM', '8 PM', '9 PM', '10 PM');
foreach($time as $key => $value)
{
$times .= '<tr><td>'.$value.'</td>';
for($a=1;$a<8;$a++)
{
$times .= '<td></td>';
}
$times .= '</tr>';
}
$daysTime .= '</tr>';
$weekDaysLabel .= '</tr>';
$weekHeader = '<p>
<a href="'.$_SERVER['PHP_SELF'].'?CalendarType=week&CurrentMonth='.date('m/d/Y', strtotime('-1 day '.$startDate)).'">Previous Week</a>
|
<a href="'.$_SERVER['PHP_SELF'].'?CalendarType=week&CurrentMonth='.date('m/d/Y', strtotime('+8 days '.$startDate)).'">Next Week</a>
</p>';
return $weekHeader.'<table id="LeoCalendar">'.$weekDaysLabel.$daysTime.$times.'</table>';
}