在php中显示一个月的几天,并将它们与mysql中的日期进行比较?

5f0d552i  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(289)

我在正确生成一个月的天数方面有问题,例如从2018-10-01到2018-10-31,并检查在“for”循环中生成的每一天是否都在mysql数据库中。
我可以生成几天,但是在从生成的循环与数据库的数据进行日期比较的条件下,它只显示来自数据库的记录,以及如何在空白记录添加日期中插入空来创建错误。
我的代码:

$conn = dbManager::getConnection();

   $tr = 1;

for($tr; $tr < $day; $tr++){

  $dt = new DateTime($start);

  $msc = $dt->format('m');

      switch ($msc) {
        case '1':
         $m = '1';
          break;
        case '2':
         $m = '2';
          break;
        case '3':
         $m = '3';
          break;
        case '4':
          $m = '4';
          break;
        case '5':
          $m = '5';
          break;
        case '6':
         $m = '6';
          break;
        case '7':
         $m = '7';
          break;
        case '8':
          $m = '8';
          break;
        case '9':
          $m = '9';
          break;
        case '10':
          $m = '10';
          break;
        case '11':
          $m = '11';
          break;
        case '12':
          $m = '12';
          break;

      }

         if($m < 10){
          $mc = '0'.$m;
         }else{
          $mc = $m;
         }

         if($tr < 10){ 
          $tr = '0'.$tr;
         }else{
          $tr = $tr;
         }

   $data = date('Y-'.$mc.'-'.$tr);

   echo "<tr><td>".$data."</td><td  style='vertical-align:middle;'>";

   $result = $conn->query("SELECT * FROM open ")or die(mysqli_error());

      while($row = mysqli_fetch_array($result)){       

           $Start = $row['Start'];
           $End = $row['End'];

           $dtb = new DateTime($Start);
           $dtb_e = new DateTime($End);
           $dtb_e->add(new DateInterval('PT1H'));

           $w= $dtb->format('Y-m-d');
           $ts = $dtb->format('H'); 
           $te = $dtb_e->format('H');

          if($w == $data){

             for($h=1;$h<24;$h++){

                 if($h >= $ts && $h <= $te){
                   if($w < date('Y-m-d')){ 
                    $color ='gray';
                   }else{
                    $color = 'green';
                   }

                   echo '<p style="display:inline;padding:5px; border:1px solid ;background:'.$color.';color:#fff;border-radius: 5px;margin-right:5px;">'.$h.':00</p>';
                 }else{
                  $color = 'gray';

                  echo '<p style="display:inline;padding:5px; border:1px solid ;background:'.$color.';color:#fff;border-radius: 5px;margin-right:5px;">'.$h.':00</p>';
                 }
              }

              if($data >= date('Y-m-d')){ 

               echo '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success"  href="edit_termin.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Edytuj</a><a class="btn btn-sm btn-danger"  href="del_open.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Usuń</a></div>';
              }else{ 
                echo '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success disabled"  href="edit_termin.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Edytuj</a><a class="btn btn-sm btn-danger disabled"  href="del_open.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Usuń</a></div>';
              }

              if($data != $data){
                        $dodaj = '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success"  href="edit_termin.php?Start='.$data.'" role="button">Dodaj</a></div>';
                         echo $dodaj;
              }else{
                echo 'x';
              }

          }else{
              if($data != $data){
                        $dodaj = '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success"  href="edit_termin.php?Start='.$data.'" role="button">Dodaj</a></div>';
                         echo $dodaj;
              }else{
                echo 'x';
              }
            echo  '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success"  href="edit_termin.php?Start='.$data.'" role="button">Dodaj</a></div>';  

          }

      }  

}
 echo "</td></tr>";

结果如下:
生成的天没有添加按钮

webghufk

webghufk1#

谢谢你的帮助。我根据您的提示修改了它,并将按钮显示在正确的位置,但它只需要上一条记录的数据库字段中的小时数。(np.start 2018-10-05 09:00 end 2018-10-05 20:00)脚本仅显示数据库中找到的每条记录的这些小时数,而不显示分配给给定日期的小时数。。我哪里会犯错?

$tr=1;

            for($tr; $tr < $day; $tr++){

              $dt = new DateTime($start);

                $msc = $dt->format('m');

                    switch ($msc) {
                      case '1':
                       $m = '1';
                        break;
                      case '2':
                       $m = '2';
                        break;
                      case '3':
                       $m = '3';
                        break;
                      case '4':
                        $m = '4';
                        break;
                      case '5':
                        $m = '5';
                        break;
                      case '6':
                       $m = '6';
                        break;
                      case '7':
                       $m = '7';
                        break;
                      case '8':
                        $m = '8';
                        break;
                      case '9':
                        $m = '9';
                        break;
                      case '10':
                        $m = '10';
                        break;
                      case '11':
                        $m = '11';
                        break;
                      case '12':
                        $m = '12';
                        break;

                    }

                       if($m < 10){
                        $mc = '0'.$m;
                       }else{
                        $mc = $m;
                       }

                       if($tr < 10){ 
                        $tr = '0'.$tr;
                       }else{
                        $tr = $tr;
                       }

              //$dt = new DateTime($start);
              //$msc = $dt->format('m');

              // $mc = $nsc; //strpad($msc, 2, '0');
              //strpad($tr, 2, '0');

              $data = date('Y-'.$mc.'-'.$tr);

              echo '<tr><td>'.$data.'</td><td>';

              // You should limit your results to the working month
              $result = $conn->query("SELECT * FROM open ".$month." ")or die(mysqli_error());

              $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
              $db_dates = array_column($rows, 'Start');
              $db_dates = array_map(function($data) { 
                $d = new DateTime($data); 
                return $d->format('Y-m-d');
              }, $db_dates);

              // Check if current date is not in the db result
              $target_date = array_search($data, $db_dates);

              if ($target_date !== FALSE) {
                  // Add your buttons

                foreach ($rows as $key) {

                  $Start = $key['Start'];
                  $End = $key['End'];
                  $id = $key['ID'];
                }

                             $str = $key['Start'];
                             $ed = $key['End'];

                             $dtb = new DateTime($str);
                             $dtb_e = new DateTime($ed);
                             $dtb_e->add(new DateInterval('PT1H'));

                             $w= $dtb->format('Y-m-d');
                             $ts = $dtb->format('H'); 
                             $te = $dtb_e->format('H');

                               for($h=1;$h<24;$h++){

                                 if($h >= $ts && $h <= $te){

                                   if($w < date('Y-m-d')){ 
                                    $color ='gray';
                                   }else{
                                    $color = 'green';
                                   }

                                   echo '<p style="display:inline;padding:5px; border:1px solid ;background:'.$color.';color:#fff;border-radius: 5px;margin-right:5px;">'.$h.':00</p>';

                                 }else{

                                  $color = 'gray';
                                  echo '<p style="display:inline;padding:5px; border:1px solid ;background:'.$color.';color:#fff;border-radius: 5px;margin-right:5px;">'.$h.':00</p>';
                                 }
                              }

                              //--------------------------------------

                              if($data >= date('Y-m-d')){ 

                                 //-------------------------
                                 $dst = $data.' '.$ts.':00';
                                 $den = $data.' '.$te.':00';

                                 //echo "SELECT * FROM open WHERE Start >= '".$dst."' AND End <= '".$den."' ";
                                 $res = $conn->query("SELECT * FROM open WHERE Start >= '".$dst."' AND End <= '".$den."' ");

                                 while($r = mysqli_fetch_array($res)){
                                  $idr = $r['ID']; //ID record

                               echo '<td><div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success"  href="edit_termin.php?Start='.$dst.'&End='.$den.'&ID='.$idr.'" role="button">Edutuj</a><a class="btn btn-sm btn-danger"  href="del_open.php?Start='.$dst.'&End='.$den.'&ID='.$idr.'" role="button">Usuń</a></div></td>';
                                 }

                              }else{ 
                                echo '<td><div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success disabled"  href="edit_termin.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$idr.'" role="button">Edytuj</a><a class="btn btn-sm btn-danger disabled"  href="del_open.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Usuń</a></div></td>';
                              }

              } else {
                // Column and map operations keep the indexes, access your record
                $row = $rows[$target_date];
                  // Rest of your logic in here
                 if($data >= date('Y-m-d')){
                     echo  '<td><div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success"  href="form_terminarz.php?Start='.$data.'" role="button">Dodaj termin</a></div></td>'; 
                  }else{
                     echo  '<td><div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success disabled"  href="form_terminarz.php?Start='.$data.'" role="button">Dodaj termin</a></div></td>'; 
                  } 
              }

              echo '<td></tr>';
            }
kadbb459

kadbb4592#

希望这能让你走。这样做的目的是一次获取所有结果,以便您能够检查数据库中是否已经存在日期:

$conn = dbManager::getConnection();
$tr = 1;

$dt = new DateTime();
$msc = $dt->format('m');
$mc = str_pad($msc, 2, '0', STR_PAD_LEFT);

// You should limit your results to the working month
$result = $conn->query("SELECT * FROM open ")or die(mysqli_error());

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$db_dates = array_column($rows, 'Start');
$db_dates = array_map(function($v) { 
  $d = new DateTime($v); 
  return $d->format('Y-m-d');
}, $db_dates);

for($tr; $tr < $day; $tr++){
  $tr = str_pad($tr, 2, '0', STR_PAD_LEFT);

  $data = date('Y-'.$mc.'-'.$tr);

  // Check if current date is not in the db result
  $target_date = array_search($data, $db_dates);

  if ($target_date === FALSE) {
      // Add your buttons
  } else {
    // Column and map operations keep the indexes, access your record
    $row = $rows[$target_date];
      // Rest of your logic in here
  }
}

相关问题