Php 我如何在月份';的总天数是否显示为新的月份天数?

Php 我如何在月份';的总天数是否显示为新的月份天数?,php,calendar,Php,Calendar,我有一个php日历在。 我希望不属于当前月份的日期显示为灰色(现在是这样),并希望它显示为上个月和下个月的日期 现在,在一个月的第一天之前出现的天数显示为负数(-1,-2,-3,依此类推),而在一个月的最后一天之后出现的天数继续显示,因此如果一个月在31日结束,那么它将显示为32、33、34,依此类推 我试图找出一个带有某种循环的条件语句,在这里我可以看到它是否大于总天数,然后做一些其他的事情。我看到的问题是,正在创建的表格单元格正在循环,因此如果我只执行$day+1,那么它将只读取33,而不是

我有一个php日历在。 我希望不属于当前月份的日期显示为灰色(现在是这样),并希望它显示为上个月和下个月的日期

现在,在一个月的第一天之前出现的天数显示为负数(-1,-2,-3,依此类推),而在一个月的最后一天之后出现的天数继续显示,因此如果一个月在31日结束,那么它将显示为32、33、34,依此类推

我试图找出一个带有某种循环的条件语句,在这里我可以看到它是否大于总天数,然后做一些其他的事情。我看到的问题是,正在创建的表格单元格正在循环,因此如果我只执行$day+1,那么它将只读取33,而不是32

这是我的密码:

for($i=0; $i< $total_rows; $i++)
{
    for($j=0; $j<7;$j++)
    {
        $day++;                 

        //if the current day is less or equal to the total days in the month           
        if($day>0 && $day<=$total_days_of_current_month)
        {
            $date_form = "$current_year/$current_month/$day";

            echo '<div class="date_has_event" href="#"><td';

            //If the date is today then give the td cell the 'today' class
            if($date_form == $today)
            {
                echo ' class="today"';
            }

            //check if any event stored for the date
            if(array_key_exists($day,$events))
            {
                //adding the date_has_event class to the <td> and close it
                echo ' class="date_has_event">'.$day;

                //adding the eventTitle and eventContent wrapped inside <span> & <li> to <ul>
                echo '<div class="events"><ul>'.$events[$day].'</ul></div>';
            }
        }   
        else //if the current day is less or more than the total days in the month 
        {
            //then create a table cell with the current day of the mont
            echo '<td class="padding">' . $day . '&nbsp;</td>'; h
        }
    }
}
($i=0;$i<$total_行;$i++)的

{

对于($j=0;$j0&&$day只需减去当前月份的天数即为正:

else //if the current day is less or more than the total days in the month 
{
    if($day > 1){
        echo '<td class="padding">' . ($day - $total_days_of_current_month) . ' </td>';      // the next month
    } else {
        echo '<td class="padding">' . $day . ' </td>';       //then create a table cell with the current day of the month
    }
}
else//如果当前日期小于或大于当月的总天数
{
如果($day>1){
回显“”。($day-$total\u days\u of\u current\u month)。“”;//下个月
}否则{
echo“.$day.”;//然后创建一个包含当月当前日期的表格单元格
}
}

这是我最近编写的日历函数的一部分,希望您能从中获得一些想法

// $month is a UNIX timestamp

// resetting to 1st of the month
$date = getdate(mktime(0, 0, 0, date('n', $month), 1, date('Y', $month)));

// resetting to first day in grid
$date = getdate(strtotime("-$date[wday] days", $date[0]));

$out = array();

$lastDay = mktime(0, 0, 0, date('n', $month), date('t', $month), date('Y', $month));

while ($date[0] <= $lastDay) {

    $row = array();

    for ($x = 0; $x <= 6; $x++) {
        $attr = array('class' => 'weekday '.low(date('D', $date[0])));
        if (date('n', $month) != $date['mon']) {
            $attr['class'].= ' prevNextMonth';
        }
        if (date('Y-m-d') == date('Y-m-d', $date[0])) {
            $attr['class'].= ' today';
        }

        $row[] = array($date['mday'], $attr);

        $date = getdate(strtotime("+1 day", $date[0]));
    }

    $out[] = $row;
}

// makes table rows out of the array, considers the $attr array as well
$out = $this->Html->tableCells($out);
$out = sprintf('<table><tbody>%s</tbody></table>', $out);
/$month是UNIX时间戳
//重置为本月1日
$date=getdate(mktime(0,0,0,日期($n',$month),1,日期($Y',$month));
//重置为网格中的第一天
$date=getdate(strotime(“-$date[wday]days”,$date[0]);
$out=array();
$lastDay=mktime(0,0,0,日期('n',$month),日期('t',$month),日期('Y',$month));
while($date[0]Html->tableCells($out);
$out=sprintf('%s',$out);

您不需要Steve的条件:而是使用

echo''日期(“j”,mktime(12,0,0,$current\u month,$day,$current\u year))。'


mktime处理超出范围的日期,我会将它们移到下一个月或上一个月,这正是您想要的。

这对于超过一个月总天数的数字很有效,但是在一个月的第一天之前的数字需要表示为31、30、29等等,但这很困难,因为有些月份有30天天和某些月份有31天。我不确定问题出在哪里。“t”会给你一个月的天数。只需要想一想,PHP已经给了你所有困难的东西。