dousi5501 2013-01-02 05:56
浏览 37

在表列中的php中回显月份的日期?

ok i'm sorry this code is all such a mess, but i have created a table with html text area forms which acts as a type of calendar for users to mark when they will be doing what on which day. i have monday - sunday shown in my code below but have 4 weeks or 4 sets of monday-sunday's in the calendar in total.

I am wondering if i am able to use a php code to place the date of each month; i.e. 1st-31st into each row/column depending on which month it is at the time?

I'd really appreciate anyones help if they could show me how i can do this and i know it doesnt look a very good way of doing it but i do need to do it this way. Thanks

<table width="100%" border="0" cellspacing="5" cellpadding="5">
      <tr>
        <th width="11.5%" align="center" valign="middle" bgcolor="#EBEBEB" scope="col">Monday</th>
        <th width="11.5%" align="center" valign="middle" bgcolor="#EBEBEB" scope="col">Tuesday</th>
        <th width="11.5%" align="center" valign="middle" bgcolor="#EBEBEB" scope="col">Wednesday</th>
        <th width="11.5%" align="center" valign="middle" bgcolor="#EBEBEB" scope="col">Thursday</th>
        <th width="11.5%" align="center" valign="middle" bgcolor="#EBEBEB" scope="col">Friday</th>
        <th width="11.5%" align="center" valign="middle" bgcolor="#EBEBEB" scope="col">Saturday</th>
        <th width="11.5%" align="center" valign="middle" bgcolor="#EBEBEB" scope="col">Sunday</th>
       </tr>
        <tr>
        <th width="11.5%" height="55" align="left" valign="middle" bgcolor="#FFFFFF" scope="col"><form action="includes/mod_profile/mod_calendar/includes/change_monday1.php" method="post" id="form1">         
     <textarea id="monday1" style="width:79px; 
        margin-top:-31.1px;
        text-align:left;
        margin-left:-3px;
        height: 60px;
        position:absolute;
        z-index:90;
        resize: none; 
        border: hidden;" textarea name="monday1" data-id="monday1" onkeyup="checkWordCount();"><?php echo $calendar['monday1'] ?></textarea>
    <input type="image" src="../PTB1/assets/img/icons/save-edit.png"class="cal-submit" width="12px" height="12px" name="submit" value="submit" id="submit"/>
    </form></th>
        <th width="11.5%" height="55" align="left" valign="middle" bgcolor="#FFFFFF" scope="col"><form action="includes/mod_profile/mod_calendar/includes/change_tuesday1.php" method="post" id="form1">            
     <textarea id="bio" style="width:79px; 
        margin-top:-31.1px;
        text-align:left;
        margin-left:-3px;
        height: 60px;
        position:absolute;
        z-index:90;
        resize: none; 
        border: hidden;" textarea name="tuesday1" data-id="tuesday1" onkeyup="checkWordCount();"><?php echo htmlspecialchars($calendar['tuesday1']); ?></textarea>
    <input type="image" src="../PTB1/assets/img/icons/save-edit.png"class="cal-submit" width="12px" height="12px" name="submit" value="submit" id="submit"/>
    </form></th>
        <th width="11.5%" height="55" align="left" valign="middle" bgcolor="#FFFFFF" scope="col"><form action="includes/mod_profile/mod_calendar/includes/change_wednesday1.php" method="post" id="form1">          
     <textarea id="wednesday1" style="width:79px; 
        margin-top:-31.1px;
        text-align:left;
        margin-left:-3px;
        height: 60px;
        position:absolute;
        z-index:90;
        resize: none; 
        border: hidden;" textarea name="wednesday1" data-id="wednesday1" onkeyup="checkWordCount();"><?php echo htmlspecialchars($calendar['wednesday1']); ?></textarea>
    <input type="image" src="../PTB1/assets/img/icons/save-edit.png"class="cal-submit" width="12px" height="12px" name="submit" value="submit" id="submit"/>
    </form></th>
        <th width="11.5%" height="55" align="left" valign="middle" bgcolor="#FFFFFF" scope="col"><form action="includes/mod_profile/mod_calendar/includes/change_thursday1.php" method="post" id="form1">           
     <textarea id="thursday1" style="width:79px; 
        margin-top:-31.1px;
        text-align:left;
        margin-left:-3px;
        height: 60px;
        position:absolute;
        z-index:90;
        resize: none; 
        border: hidden;" textarea name="thursday1" data-id="thursday1" onkeyup="checkWordCount();"><?php echo htmlspecialchars($calendar['thursday1']); ?></textarea>
    <input type="image" src="../PTB1/assets/img/icons/save-edit.png"class="cal-submit" width="12px" height="12px" name="submit" value="submit" id="submit"/>
    </form></th>
        <th width="11.5%" height="55" align="left" valign="middle" bgcolor="#FFFFFF" scope="col"><form action="includes/mod_profile/mod_calendar/includes/change_friday1.php" method="post" id="form1">         
     <textarea id="friday1" style="width:79px; 
        margin-top:-31.1px;
        text-align:left;
        margin-left:-3px;
        height: 60px;
        position:absolute;
        z-index:90;
        resize: none; 
        border: hidden;" textarea name="friday1" data-id="friday1" onkeyup="checkWordCount();"><?php echo htmlspecialchars($calendar['friday1']); ?></textarea>
    <input type="image" src="../PTB1/assets/img/icons/save-edit.png"class="cal-submit" width="12px" height="12px" name="submit" value="submit" id="submit"/>
    </form></th>
        <th width="11.5%" height="55" align="left" valign="middle" bgcolor="#FFFFFF" scope="col"><form action="includes/mod_profile/mod_calendar/includes/change_saturday1.php" method="post" id="form1">           
     <textarea id="saturday1" style="width:79px; 
        margin-top:-31.1px;
        text-align:left;
        margin-left:-3px;
        height: 60px;
        position:absolute;
        z-index:90;
        resize: none; 
        border: hidden;" textarea name="saturday1" data-id="saturday1" onkeyup="checkWordCount();"><?php echo htmlspecialchars($calendar['saturday1']); ?></textarea>
    <input type="image" src="../PTB1/assets/img/icons/save-edit.png"class="cal-submit" width="12px" height="12px" name="submit" value="submit" id="submit"/>
    </form></th>
        <th width="11.5%" height="55" align="left" valign="middle" bgcolor="#FFFFFF" scope="col"><form action="includes/mod_profile/mod_calendar/includes/change_sunday1.php" method="post" id="form1">         
     <textarea id="sunday1" style="width:79px; 
        margin-top:-31.1px;
        text-align:left;
        margin-left:-3px;
        height: 60px;
        position:absolute;
        z-index:90;
        resize: none; 
        border: hidden;" textarea name="sunday1" data-id="sunday1" onkeyup="checkWordCount();"><?php echo htmlspecialchars($calendar['sunday1']); ?></textarea>
    <input type="image" src="../PTB1/assets/img/icons/save-edit.png"class="cal-submit" width="12px" height="12px" name="submit" value="submit" id="submit"/>
    </form></th>
       </tr>
       <tr>
  • 写回答

1条回答 默认 最新

  • dpb75177 2013-01-02 06:56
    关注

    This will position your dates in the correct columns - just add whatever you need to the inner loop of printCalendar():

    <?php
    function getCalendarRows($year,$month)
    {
        $result=array();
        $dt=new DateTime(sprintf('%04d-%02d-01',$year,$month));
        $weekDay=$dt->format('w'); //Weekday of the first
        $nextMonth=$month==12?1:$month+1;
        $nextMonthYear=$nextMonth==1?$year+1:$year;
        $dt=new DateTime(sprintf('%04d-%02d-01',$nextMonthYear,$nextMonth));
        $dt->sub(new DateInterval('P1D')); //last day of the month
        $row=array();
        $lastDay=$dt->format('j');
        //Start the first row with empty columns (false)
        for($i=0; $i<$weekDay; $i++)$row[]=false; 
        for($i=1; $i<=$lastDay; $i++)
        {
            $row[]=$i;
            $weekDay++;
            if($weekDay>6)
            {
                $result[]=$row;
                $row=array();
                $weekDay=0;
            }
        }
        if($weekDay>0) 
        {
            //Fill up the last row with empty columns
            for($i=$weekDay+1; $i<7; $i++) $row[]=false;
            $result[]=$row;
        }
        return $result;
    }
    
    function printCalendar($year,$month)
    {
        $rows=getCalendarRows($year,$month);
        echo '
    <table>
        <tr>
            <th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th>
        </tr>';
        foreach($rows as $row)
        {
            echo '
        <tr>';
            foreach($row as $weekday=>$date)
            {
                echo '
            <td width="50px" height="50px">'.($date===false?'':$date).'</td>';
    //Put whatever else you want in here, you have $year, $month, $date, and $weekday (0 to 6)
    //NOTE - if $date===false, this is an empty column
            }
            echo '
        </tr>';
        }
        echo '
    </table>';
    }
    echo '
    <h1>Jan 2013</h1>';
    printCalendar(2013,01);
    echo '
    <h1>Feb 2013</h1>';
    printCalendar(2013,02);
    echo '
    <h1>Mar 2013</h1>';
    printCalendar(2013,03);
    echo '
    <h1>Dec 2013</h1>';
    printCalendar(2013,12);
    ?>
    

    Here it is at phpfiddle: http://phpfiddle.org/main/code/3vf-1z0 (I changed the DateTime constructor calls because sprintf didn't work on phpfiddle)

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 基于52单片机的酒精浓度检测系统加继电器和sim800
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答