简体   繁体   English

PHP。 当用户选择此选项时,显示所有12个月的日历

[英]PHP. displaying a calendar for all 12 months when the user selects this option

I have an assignment that includes a "select whole year" checkbox. 我有一个包括“选择全年”复选框的作业。 When the user checks this box and submits, 12 calendars will display, one for each month. 当用户选中此框并提交时,将显示12个日历,每个月一个。 I have this working except for the month of June. 除了六月份,我有这个工作。 All calendars, except June, display perfectly. 除6月外,所有日历均显示完美。 The days for June are not in calendar form but rather spread out across the page. 6月的日期不是日历形式,而是分布在整个页面上。 I don't understand how this is occurring or how to fix it. 我不知道这种情况如何发生或如何解决。 I would appreciate some help. 我将不胜感激。

 if ($isPostBack) { $whole_year = filter_input(INPUT_POST, 'whole_year'); $select_year = filter_input(INPUT_POST, 'select_year', FILTER_VALIDATE_FLOAT); $select_month = filter_input(INPUT_POST, 'select_month', FILTER_VALIDATE_FLOAT); if ($whole_year) { $months_to_show = NUMBER_MONTH; } else { $months_to_show = 1; } $start_date = date_create_from_format('Ym-d', "{$select_year}-{$select_month}-01"); while ($months_to_show > 0) { $calendar = <<<CALENDAR <table> <tr><th colspan="7" style="text-align:center">{$start_date->format('F')} {$start_date->format('Y')}</th></tr> <tr> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> </tr> <tr> CALENDAR; echo $calendar; $week_starts = $start_date->format('w'); for ($fillers = 0; $fillers < $week_starts; $fillers++) { echo "<td></td>"; } $day_of_week = $week_starts; for ($day_of_month = 1; $day_of_month <= $start_date->format('t'); $day_of_month++) { if ($day_of_week === 0) { echo '<tr>'; } echo "<td><a href='date.php?date={$select_year}-{$start_date->format('M')}-{$day_of_month}'>{$day_of_month}</a></td>"; if ($day_of_week === 6) { echo '</tr>'; $day_of_week = 0; } else { $day_of_week++; } } echo "</table>"; $start_date->modify('+1 month'); $months_to_show--; } } else { } ?> </body> </html> 

Your problem comes if the first day of the month is Sunday. 如果一个月的第一天是星期日,就会出现问题。 You use the identical operator === that checks if the type of the two variables are the same too. 您使用相同的运算符===来检查两个变量的类型是否也相同。 However, your variable $week_starts is first initialize with $start_date->format('w'); 但是,变量$week_starts首先用$start_date->format('w');初始化$start_date->format('w'); that always returns a string. 总是返回一个字符串。 So the condition if ($day_of_week === 6) returns false . 因此条件if ($day_of_week === 6)返回false Then, when PHP will evaluate $day_of_week++; 然后,PHP将何时评估$day_of_week++; , it will cast the variable to an integer. ,它将变量转换为整数。 You will have the serie '6', 7, 8, 9, ... and your table row will never be closed. 您将拥有系列'6', 7, 8, 9, ...并且您的表行将永远不会关闭。

If the first day of the month is not a sunday, the variable will be cast to an integer before 6 and things will go fine : '3', 4, 5, 6, 0, 1, 2, 3, ... 如果该月的第一天不是星期日,则该变量将被强制转换为6之前的整数,并且一切正常: '3', 4, 5, 6, 0, 1, 2, 3, ...

So, two solutions : 因此,有两种解决方案:

  • Cast $week_starts to an integer at the beginning : $week_starts = intval($start_date->format('w')); 在开头将$week_starts转换为整数: $week_starts = intval($start_date->format('w'));
  • Use the simple equal operator == that will not check the types of the two variables : if ($day_of_week == 6) 使用简单的等于运算符== ,它将不检查两个变量的类型: if ($day_of_week == 6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM