【问题标题】:PHP For Loop repeating an arrayPHP For循环重复一个数组
【发布时间】:2018-06-11 09:19:23
【问题描述】:

我正在尝试创建一个日历系统,该系统将显示在某些日子预订了哪些工作,我希望它是一个水平日历,将月份的所有日期作为表格行和一周中的几天作为下一行。我已经对第一行进行了排序,但我需要让第二行重复一周中的几天的数组,直到月末。

      //Establish How many days are in the current Month
  $currentMonthDays = date("t");




  $daysOfWeek = array('M','T','W','T','F','Sa','Su');

      echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="ffffff">
            <tr>';


                    for ($i = 1; $i <= $currentMonthDays; $i++)
                    {

                        echo '<td width="30" align="center">'.$i.'</td>';
                    }

      echo '</tr><tr>';


                    foreach($daysOfWeek as $day){
                    echo '<td width="30" align="center">'.$day.'</td>';
                    }


      echo '</tr>';

【问题讨论】:

    标签: php arrays loops for-loop calendar


    【解决方案1】:

    试试这个:

     //Establish How many days are in the current Month
      $currentMonthDays = date("t");
      $daysOfWeek = array('M','T','W','T','F','Sa','Su');
          for ($i = 0; $i <= $currentMonthDays-1; $i++){
              $arrayWeekDays[] = $daysOfWeek[($i%7)];
          }
          echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="ffffff">
                <tr>';
    
    
                        for ($i = 1; $i <= $currentMonthDays; $i++)
                        {
    
                            echo '<td width="30" align="center">'.$i.'</td>';
                        }
    
          echo '</tr><tr>';
    
    
                        foreach($arrayWeekDays as $day){
                        echo '<td width="30" align="center">'.$day.'</td>';
                        }
    
    
          echo '</tr>';
    

    干杯:)

    【讨论】:

    • 超级@waren0809 太棒了,干得好
    • 谢谢@Muthusamy :)
    • 谢谢,太好了,我怀疑在某些时候我必须使用模数,但我的大脑在转圈圈。现在我需要开始处理所有其他的小问题,比如确定月份从哪一天开始,需要多少个单元格等。
    【解决方案2】:

    试试下面的代码

        $currentMonthDays = date("t");
    
        $daysOfWeek = array('M', 'T', 'W', 'T', 'F', 'Sa', 'Su');
    
        echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="ffffff">
            <tr>';
    
        for ($i = 1; $i <= $currentMonthDays; $i++) {
    
            if ($i % 7 == 1) 
                echo '</tr><tr>';
            echo '<td width="30" align="center">' . $i . '</td>';
        }
    
        echo '</tr><tr>';
    
    
        foreach ($daysOfWeek as $day) {
                echo '<td width="30" align="center">' . $day . '</td>';
        }
    
    
        echo '</tr>';
    

    也许对你有帮助。

    【讨论】:

      【解决方案3】:

      最简单的方法是使用 DateTime 类和格式化方法来获取天数并为您格式化日期。

      $currentMonthDays = date('t');
      $month = date('m');
      $year = date( 'Y');
      
      for ($i = 1; $i <= $currentMonthDays; $i++) {
          echo '<td width="30" align="center">'.$i.'</td>';
      }
      $day = new DateTime();
      for ($i = 1; $i <= $currentMonthDays; $i++) {
          $day->setDate( $year, $month, $currentMonthDays);
          // this outputs first 3 letters of day, but you can truncate if you want..
          echo '<td width="30" align="center">'.$day->format('D').'</td>';
      }
      

      您还可以通过合并两个 tds 并通过在月份的日期和星期几之间添加 br 元素或使用 CSS 格式化来格式化单元格来避免需要 2 个循环。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-25
        • 2021-08-27
        • 1970-01-01
        • 2021-03-28
        • 2016-01-16
        • 1970-01-01
        相关资源
        最近更新 更多