为什么它没有显示几个月的所有时间?

时间:2019-07-31 17:38:49

标签: php laravel

我使用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

0 个答案:

没有答案