我试图制作一个显示本月的日历,然后在结束行中填写剩余日期,其中包含下一个月和前几个月的日期。
我设法让它显示下个月的日期,问题是我需要它来显示前几个月的日期。
如果有人设法找出问题,请添加问题以及如何设法解决问题,以便将来帮助我。
我也会添加日历的图像。
剧本:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
// Get current month dates
$days_count = date('t');
$current_day = date('d');
$week_day_first = date('N', mktime(0, 0, 0, date('m'), 1, date('Y')));
// Get previous month dates
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}else if($current_day == $d){
echo '<div class="current-day"><span class="given-date">'.$d.'</span></div>';
}else{
echo '<span class="given-date">'.$d.'</span>';
}
}else{
//Here comes previous dates
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
答案 0 :(得分:1)
当您计算$week_day_first
时,您可以将从该月第一天计算的时间保存到变量中。
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
然后您可以稍后重复使用它来计算月初之前的天数偏移量。
date('d', strtotime("$offset day",$time_first_day_of_month))
全部放在一起:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
// Get current month dates
$days_count = date('t');
$current_day = date('d');
// save time of first day for later use
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
$week_day_first = date('N', $time_first_day_of_month);
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
// next month's dates
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}
// today
else if($current_day == $d){
echo '<div class="current-day"><span class="given-date">'.$d.'</span></div>';
}
// this month's dates
else{
echo '<span class="given-date">'.$d.'</span>';
}
}
// last month's dates
else{
//Here comes previous dates
$offset = $d - 1;
echo '<span class="given-date">'.date('d', strtotime("$offset day",$time_first_day_of_month)).'</span>';
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
此输出结果:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<tr><td valign="top" width="14.2857%" class="disabled"><span class="given-date">30</span></td><td valign="top" width="14.2857%" class="disabled"><span class="given-date">31</span></td><td valign="top" width="14.2857%"><span class="given-date">1</span></td><td valign="top" width="14.2857%"><span class="given-date">2</span></td><td valign="top" width="14.2857%"><span class="given-date">3</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">4</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">5</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">6</span></td><td valign="top" width="14.2857%"><span class="given-date">7</span></td><td valign="top" width="14.2857%"><span class="given-date">8</span></td><td valign="top" width="14.2857%"><span class="given-date">9</span></td><td valign="top" width="14.2857%"><span class="given-date">10</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">11</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">12</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">13</span></td><td valign="top" width="14.2857%"><span class="given-date">14</span></td><td valign="top" width="14.2857%"><span class="given-date">15</span></td><td valign="top" width="14.2857%"><span class="given-date">16</span></td><td valign="top" width="14.2857%"><div class="current-day"><span class="given-date">17</span></div></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">18</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">19</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">20</span></td><td valign="top" width="14.2857%"><span class="given-date">21</span></td><td valign="top" width="14.2857%"><span class="given-date">22</span></td><td valign="top" width="14.2857%"><span class="given-date">23</span></td><td valign="top" width="14.2857%"><span class="given-date">24</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">25</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">26</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">27</span></td><td valign="top" width="14.2857%"><span class="given-date">28</span></td><td valign="top" width="14.2857%"><span class="given-date">29</span></td><td valign="top" width="14.2857%"><span class="given-date">30</span></td><td valign="top" width="14.2857%" class="disabled">1</td><td valign="top" width="14.2857%" class="disabled" class="week-day">2</td><td valign="top" width="14.2857%" class="disabled" class="week-day">3</td></tr></table>
答案 1 :(得分:0)
这是使用DateTime类的代码,这使得它非常易读:
$today = new DateTime('today'); // only change this line to test other months
$this_month = $today->format('M');
$date = clone $today;
$date->modify('first day of this month')->modify('+1 day')->modify('last Monday');
do {
echo '<tr>';
for ($weekday = 0; $weekday < 7; $weekday++){
$out = str_replace($this_month, '', $date->format('M'));
$content = "<span class='given-date'>$out {$date->format('d')}</span>";
if ($today == $date) $content = "<div class='current-day'>$content</div>";
$class = ($out ? 'disabled ' : '') . ($weekday > 4 ? 'week-day' : '');
echo "<td valign='top' width='14.2857%' class='$class'>$content</td>";
$date->modify('+1 day');
}
echo '</tr>';
} while ($date->format('M') == $this_month);
答案 2 :(得分:0)
虽然这已经很老了,但我发现 Jeff's 解决方案非常有用。我稍微重构了它并把它放在这个 Gist 中,我希望有人觉得它有用。部分内容在这里:
/**
* Return part of an HTML table containing all the days
* in the current month, plus padd it with next and
* previous months days if needed to fill out the grid
*
*
* @param date $date date object containing month to display
*
* @return string
*/
function get_calendar_days_in_month_html($date) {
$date_format = "Y-m-d";
$days_count = $date->format('t'); //Get number of days in this month
$weeks_count = ceil($days_count / 7); //How many weeks in this month?
$total_cells = $weeks_count * 7;
//clone is used or we literally are modifying the $date variable
$first_date_of_month = clone $date->modify('first day of this month');
$first_day_of_month = $first_date_of_month->format("N"); //returns 1-7 EG Mon-Fri
$first_date_of_grid = $first_date_of_month ->modify('-' . $first_day_of_month . ' days');
$todays_date = new DateTime();
$todays_date_str = $todays_date->format($date_format);
$selected_date_str = $date->format($date_format);
$day_of_week = 1; //FIXME: allow starting with Sunday or whatever
$ht = '<tr>';
for ($cell=1; $cell <= $total_cells ; $cell++) {
$classes = []; //CSS classes
$current_date = $first_date_of_grid->modify("+1 day");
$current_date_str = $current_date->format($date_format);
if($current_date_str == $todays_date_str) $classes[] = "today";
if($selected_date_str == $todays_date_str) $classes[] = "selected-date";
/* if current date is not from this month (EG previous or next month) then give
it a special class, you might want to grey it out or whatever */
if($date->format("m") !== $current_date->format("m")) $classes[] = "grid-filler";
$ht .= '
<td date="' . $current_date_str . '" class="' . implode(" ", $classes) . '">' . $current_date->format("j") . '</td>';
$day_of_week ++;
if($day_of_week == 8) {
$ht .= '</tr>';
if($cell < $total_cells) $ht .= '<tr>';
$day_of_week = 1;
}
}
return $ht;
}