douqiao1413 2015-08-11 18:50
浏览 72

如何将日期与我的申请中的日期相匹配?

This may seem a little basic but here is my issue.

function output_calendar($month, $year) {

    /* draw table */
    $calendar = '<table class="table table-bordered" cellpadding="0" cellspacing="0" class="calendar">';

    /* table headings */
    $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    $calendar.= '<tr><td>'.implode('</td><td>',$headings).'</td></tr>';

    /* days and weeks vars now ... */
    $running_day = date('w',mktime(0,0,0,$month,1,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;
    $day_counter = 0;
    $dates_array = array();

    /* row for week one */
    $calendar.= '<tr>';

    /* print "blank" days until the first of the current week */
    for($x = 0; $x < $running_day; $x++):
        $calendar.= '<td> </td>';
        $days_in_this_week++;
    endfor;

    /* keep going with days.... */
    for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<td style="width:14%;height:10%;">';
            /* add in the day number */
            $calendar.= '<div>'.$list_day.'</div>';

            /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
            $calendar.= str_repeat('<p> </p>',2);

        $calendar.= '</td>';
        if($running_day == 6):
            $calendar.= '</tr>';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<tr>';
            endif;
            $running_day = -1;
            $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;
    endfor;

    /* finish the rest of the days in the week */
    if($days_in_this_week < 8):
        for($x = 1; $x <= (8 - $days_in_this_week); $x++):
            $calendar.= '<td> </td>';
        endfor;
    endif;

    /* final row */
    $calendar.= '</tr>';

    /* end the table */
    $calendar.= '</table>';

    /* all done, return result */
    return $calendar; 
}

Front End JS

                    $(document).ready(function () {
                        $(document).on('click', '#next', function() {
                            $("#main").empty();
                            if(display_month < 12) {
                            display_month++;
                            } else {
                            display_month = 1;
                            display_year++;
                            }
                            getCal(display_month, display_year);
                            $('#title').empty();
                            $('#title').html(months[display_month].concat(' ').concat(display_year));
                        });  
                        $(document).on('click', '#last', function() {
                            $("#main").empty();
                            if(display_month > 1) {
                            display_month--;
                            } else {
                            display_month = 12;
                            display_year--;
                            }
                            getCal(display_month, display_year);
                            $('#title').empty();
                            $('#title').html(months[display_month].concat(' ').concat(display_year));
                        }); 

                    });

                    // handles the click event, sends the query
                    function getCal(v1, v2) {
                    $.ajax({
                        url: "calGrab.php",
                       type: 'POST',
                       dataType: 'html',
                       data: content,
                        data: { 
                            "month": display_month, 
                            "year": display_year, 
                        },
                    }).done(function ( data ) {
                      $('#main').empty();
                      $('#main').append(data);
                    });
                    }

How do I go about correctly lining up the date with the associated day. For example, August starts on a Saturday for 2015. Are there any known techniques for this?

Please see prototype: http://calendar.conneraiken.com

  • 写回答

1条回答 默认 最新

  • duanfang5849 2015-08-11 19:00
    关注

    You'd need 3 separate loops:

    a) loop to pad calendar table with final days of previous month
    b) loop to output current month
    c) loop to pad calendar table with first days of next month

    e.g.

     Su M  T   W  Th  F Sa
     28 29 30  1  2   3  4   // 1st week of current month
     a  a  a   b  b   b  b   // loops used
          ....               // full weeks in current month, all loop 'b'
        28 29 30  1   2  3   // final week of current month
        b  b   b  c   c  c   //  loops used
    
    $first = mktime(0,0,0,$month,1,$year);
    $dow = date('w', $first);
    
    # pad calendar for "end of last month"
    echo '<tr>';
    for($i = 0; $i < $dow; $i++) {
       echo '<td></td>';
    }
    ... loop to output actual days in current month
    ... loop to pad final week with "next month" days to complete the row
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c