首页 > 解决方案 > 为什么它不显示几个月的所有日子?

问题描述

我使用 laravel 制作了一个日历,但它不适用于所有月份,它显示了一些月份而其他月份则没有

这是应用程序运行良好(仅几个月)时的外观 https://i.imgur.com/hfhoQ70.png

如果我选择下个月或上个月会发生这种情况

https://i.imgur.com/Qg6bvtx.png

// Get prev & next month
if ($dat) {
    $ym = $dat;
} else {
    // This month
    $ym = date('Y-m');
}
// Check format
$timestamp = strtotime($ym . '-01');  // the first day of the month
if ($timestamp === false) {
    $ym = date('Y-m');
    $timestamp = strtotime($ym . '-01');
}
// Today (Format:2019-07-30)
$today = date('Y-m-j');
$current= date('Y-m');
// Title (Format:July, 2019)
$title = date('F, Y', $timestamp);
// Create prev & next month link
$prev = date('Y-m', strtotime('-1 month', $timestamp));
$next = date('Y-m', strtotime('+1 month', $timestamp));
// Number of days in the month
$day_count = date('t', $timestamp);
$exploded = explode('-',$dat);
$count = cal_days_in_month(CAL_GREGORIAN,$exploded[1],$exploded[0]);
// 1:Mon 2:Tue 3: Wed ... 7:Sun
$str = date('N', $timestamp);
// Array for calendar
$weeks = [];
$week = '';
$day_count=(int)$count;
// Add empty cell(s)
$week .= str_repeat('<div class="col-xs-1 grid-cell "><div><div><span>', $str - 1);
for ($day = 1; $day <= $day_count; $day++, $str++) {
    $date = $ym . '-' . $day;
    if ($today == $date) {
        $week .= '<div class="col-xs-1 grid-cell " style="background-color: #dddada;"><div><div><span>';
    } else {
        $week .= '<div class="col-xs-1 grid-cell"><div><div><span>';
    }
    $week .= $day . '</span></div></div></div>';

    // Sunday OR last day of the month
    if ($str % 7 == 0 || $day == $day_count) {
        // last day of the month
        if ($day == $day_count && $str % 7 != 0) {
            // Add empty cell(s)
            $week .= str_repeat('<span>', 7 - $str % 7);
        }
        $weeks[] = '<div class="row calendar-week">' . $week . '</div>';
        $week = '';
    }
}
return view('TL_Views.index')->with(['weeks'=>$weeks,'title'=>$title,'prev'=>$prev,'next'=>$next,'current'=>$current,'count'=>$count]);

这是视图中的代码

@foreach ($weeks as $day)
    {!! $day !!}
@endforeach

标签: phplaravel

解决方案


推荐阅读