Php 生成两个日期之间年、月和日的平面日历布局

Php 生成两个日期之间年、月和日的平面日历布局,php,date,calendar,Php,Date,Calendar,我正在尝试生成一个从年、月、日到给定日期的平面日历布局。它应该是一个表格格式,顶部第一行是年份,然后是下面日历格式中每年的月份。与您在甘特图中看到的类似 我一直在寻找一个php类,这是相对简单的,我可以使用,但找不到一个能做到这一点 到目前为止,我遇到的最接近的代码如下,它显示了一个月一次我如何修改它来完成我所寻找的任务 <?php $date1 = new DateTime("2007-03-24"); $date2 = new DateTime("2009-06-26"); $int

我正在尝试生成一个从年、月、日到给定日期的平面日历布局。它应该是一个表格格式,顶部第一行是年份,然后是下面日历格式中每年的月份。与您在甘特图中看到的类似

我一直在寻找一个php类,这是相对简单的,我可以使用,但找不到一个能做到这一点

到目前为止,我遇到的最接近的代码如下,它显示了一个月一次我如何修改它来完成我所寻找的任务

<?php

$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);

echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";

$date = strtotime("2007-03-24");

generateCalendar($date);

function generateCalendar($date){

//This puts the day, month, and year in separate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;

echo $day_of_week;

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero

switch($day_of_week){

    case "Sun": $blank = 0; break;

    case "Mon": $blank = 1; break;

    case "Tue": $blank = 2; break;

    case "Wed": $blank = 3; break;

    case "Thu": $blank = 4; break;

    case "Fri": $blank = 5; break;

    case "Sat": $blank = 6; break;

}

//We then determine how many days are in the current month

$days_in_month = cal_days_in_month(0, $month, $year) ;

//Here we start building the table heads

echo "<table border=1 width=294>";

echo "<tr><th colspan=7> $title $year </th></tr>";

echo "<tr><td width=42>S</td><td width=42>M</td><td
width=42>T</td><td width=42>W</td><td width=42>T</td><td
width=42>F</td><td width=42>S</td></tr>";



//This counts the days in the week, up to 7

$day_count = 1;



echo "<tr>";

//first we take care of those blank days

while ( $blank > 0 )

{

    echo "<td></td>";

    $blank = $blank-1;

    $day_count++;

}

//sets the first day of the month to 1

$day_num = 1;



//count up the days, untill we've done all of them in the month

while ( $day_num <= $days_in_month )

{

    echo "<td> $day_num </td>";

    $day_num++;

    $day_count++;



    //Make sure we start a new row every week

    if ($day_count > 7)

    {

        echo "</tr><tr>";

        $day_count = 1;

    }

}

//Finaly we finish out the table with some blank details if needed

while ( $day_count >1 && $day_count <=7 )

{

    echo "<td> </td>";

    $day_count++;

}


echo "</tr></table>";

}

这将为您的表生成标题:

<?

function daysAndDaynames($datestart, $dateend) {
    while ($datestart <= $dateend) {
      $dateExpl=explode("-",$datestart);
      $dayname=date("D", strtotime($datestart));
      $row.="<td>" . $dateExpl[2] . "</td>";
      $row2.="<td>" . $dayname[0] . "</td>";
      $datestart=date('Y-m-d', strtotime('+1 day', strtotime($datestart)));
    }
    return "<tr>" . $row . "</tr><tr>" . $row2 . "</tr>";
}

function months($datestart, $dateend) {
    $dateExpl=explode("-",$datestart);
    $curMonth=$dateExpl[1];
    $colspan=0;
    $monthname=date("F", strtotime($datestart));
    while ($datestart <= $dateend) {
        $dateExpl=explode("-",$datestart);
        $monthnr=$dateExpl[1];
        if ($curMonth <> $monthnr) {
            $row.="<td colspan='$colspan'> $monthname</td>";
            $curMonth=$monthnr;
            $monthname=date("F", strtotime($datestart));
            $colspan=1;
        } else $colspan++;
        $datestart=date('Y-m-d', strtotime('+1 day', strtotime($datestart)));
    }
    $row.="<td colspan='$colspan'>$monthname</td>";
    return "<tr>" . $row . "</tr>";
}

function years($datestart, $dateend) {
    $dateExpl=explode("-",$datestart);
    $curYear=$dateExpl[0];
    $colspan=0;
    while ($datestart <= $dateend) {
        $dateExpl=explode("-",$datestart);
        $yearnr=$dateExpl[0];
        if ($curYear <> $yearnr) {
            $row.="<td colspan='$colspan'> $curYear</td>";
            $curYear=$yearnr;
            $colspan=1;
        } else $colspan++;
        $datestart=date('Y-m-d', strtotime('+1 day', strtotime($datestart)));
    }
    $row.="<td colspan='$colspan'>$curYear</td>";
    return "<tr>" . $row . "</tr>";
}   


$datestart='2011-11-01';
$dateend='2013-05-15';


echo "<table border='1'>";
$row1=years($datestart, $dateend);
$row2=months($datestart,$dateend);
$row3=daysAndDaynames($datestart,$dateend);

echo $row1. $row2 . $row3;
echo "</table>";

这是代码Iv,因此顶部的间隔代码毫无意义。一个月中的几天应该如何显示?你能给我们显示想要的输出吗?输出应该是一种类似于jquery甘特图网格的网格格式,天数沿直线1到30等运行得非常好