基于php周的日历月初

时间:2018-06-05 08:05:24

标签: php

最后一个问题我想从MySQL添加事件,但我不能让循环变得正常。他们现在循环日历3次,日历中的enter image description here日期为30次。如何让while循环循环1次?如果那不是代码中的重大变化。看看图片,它解释了什么是错误的。

  $sql = "SELECT DATE_FORMAT(date,'%d') AS dateformat FROM test";
    $result = mysqli_query($con, $sql);

     while($row=mysqli_fetch_array($result)){
    $week_number = 22;
    $year = 2018;

    $today = mktime(0, 0, 0, date("n"), date("d"), date("Y"));
    $curMonth = date("6", $today);
    $curDay = date("25", $today);


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

      echo "<tr>";
    for ($day = 1; $day <= 7; $day++) {

         $datetime = strtotime($year."W".$week_number.$day);
         $month = date('n', $datetime);
         $daysnumber = date('d', $datetime);

         $sqldate = $row['dateformat'];

          echo $sqldate ;


    if ($curMonth === $month && $daysnumber === $curDay) {
          echo"<td width=50 bgcolor='#f44242'>$daysnumber</td>";


          } elseif($curMonth === $month && $daysnumber == $sqldate) {

          echo"<td width=50 bgcolor='#1e8e8e'>$sqldate</td>";


          } elseif($curMonth === $month) {

          echo"<td width=50 bgcolor='#ffffff'>$daysnumber</td>";
        } else {

          echo"<td width=50 bgcolor='#ffffff'></td>";
        }
      }

          echo "</tr>";
    $week_number++;


}}

1 个答案:

答案 0 :(得分:0)

不显示数字03,因为此代码

elseif ($daysnumber>=$today){
    echo"<td bgcolor='#ffffff'></td>" ;    
}

$ today = 2,$ daysnumber = 03

的当前值

我不理解你的要求,

<?
$week_number = 22;
$year = 2018;
// mktime(0, 0, 0, date("m"), date("d") , date("Y"));
$today = mktime(0, 0, 0, date("m"), '2', date("Y"));
// 1 through 12
$curMonth = date("n", $today);
// 1 to 31
$curDay = date("j", $today);
// create table
echo "<table border=1>";
// showing 4 weekly week 22~
for ($i = 1; $i <= 4; $i++) {
  echo "<tr>";
  for ($day = 1; $day <= 7; $day++) {
    $datetime = strtotime($year."W".$week_number.$day);
    // 1 to 31
    $month = date('n', $datetime);
    // 1 through 12
    $daysnumber = date('j', $datetime);
    var_dump($month . '/'. $daysnumber);
    if ($curMonth === $month && $daysnumber === $curDay) {
      echo"<td width=50 bgcolor='#f44242'>$daysnumber</td>";
    } elseif($curMonth === $month) {
      // other days
      echo"<td width=50 bgcolor='#8e8e8e'>$daysnumber</td>";
    } else {
      // other month
      echo"<td width=50 bgcolor='#ffffff'>$daysnumber</td>";
    }
  }
  echo "</tr>";
  $week_number++;
}

echo "</table>";