codeigniter Phpspreadsheet导入excell当一个空的日期格式

f1tvaqid  于 2022-12-06  发布在  PHP
关注(0)|答案(1)|浏览(151)

日安晚上好我的目标是当excel有一个空日期时它将在数据表中显示一个空输出,但在我导入excel空数据后它显示01/01/1970我使用strtetime格式empty data in my excellDisplay in my datable
下面是我在datable中显示的代码

<thead>
                                        <tr>
                                            <th class="text-center align-middle">Action</th>
                                            <th class="text-center align-middle ">Last Name</th>
                                            <th class="text-center align-middle ">First Name</th>
                                            <th class="text-center align-middle ">Middle Name</th>
                                            <th class="text-center align-middle ">Zone</th>
                                            <th class="text-center align-middle ">Birth Day</th>
                                            <th class="text-center align-middle ">Age</th>
                                            <th class="text-center align-middle">Service Give</th>
                                            <th class="text-center align-middle ">LMP</th>
                                            <th class="text-center align-middle ">EDC</th>
                                            <th class="text-center align-middle ">GPA</th>
                                            <th class="text-center align-middle ">REMARKS</th>
                                            <th class="text-center align-middle ">Teenage Pregnancy</th>
                                            
                                        </tr>
                                    </thead>
                                    <tbody>
                                     <?php
                                    if($records){ 
                                        foreach ($records as $record):
                                    ?>
                                    <tr>
                                           <td class="text-center align-middle">
                                            
                                            <a href="<?php echo base_url('dashboard/editteen/'.$record->id); ?>" class="btn btn-primary "title="Edit Record">
                                            <i class="fas fa-edit"></i>
                                            </a>
                                           </td>
                                          <td class="text-center align-middle"><?php echo $record->lastname;?></td>
                                          <td class="text-center align-middle"><?php echo $record->firstname;?></td>
                                          <td class="text-center align-middle"><?php echo $record->middlename;?></td>
                                          <td class="text-center align-middle"><?php echo $record->zone;?></td>
                                          <td class="text-center align-middle">
                                             <?php echo !empty($record->bday) ? date('m/d/Y', strtotime($record->bday)) : '';?>
                                          </td>
                                          <td class="text-center align-middle"><?php echo $record->age;?></td>
                                          <td class="text-center align-middle"><?php echo $record->servicegive;?></td>
                                          <td class="text-center align-middle">
                                             <?php echo !empty($record->lmp) ? date('m/d/Y', strtotime($record->lmp)) : '';?>
                                          </td>
                                          <td class="text-center align-middle">
                                             <?php echo !empty($record->edc) ? date('m/d/Y', strtotime($record->edc)) : '';?>
                                          </td>
                                          <td class="text-center align-middle"><?php echo $record->gpa;?></td>
                                          <td class="text-center align-middle"><?php echo $record->remarks;?></td>
                                          <td class="text-center align-middle"><?php echo $record->teenagepregnancy;?></td>
                                    </tr>

下面是我的代码

For loop in
        if($sheetcount>1)
            {
              $data=array();
              for ($i=1; $i < $sheetcount; $i++) { 
                $lastname=$sheetdata[$i][1];
                $firstname=$sheetdata[$i][2];
                $middlename=$sheetdata[$i][3];
                $zone=$sheetdata[$i][4];
                $bday=date('y-m-d',strtotime($sheetdata[$i][5]));
                $age=$sheetdata[$i][6];
                $servicegive=$sheetdata[$i][7];
                $lmp=date('y-m-d',strtotime($sheetdata[$i][8]));
                $edc=date('y-m-d',strtotime($sheetdata[$i][9]));
                $gpa=$sheetdata[$i][10];
                $remarks=$sheetdata[$i][11];
                $teenagepregnancy=$sheetdata[$i][12];
       
        
set of data array
        $data[]=array(
          'lastname'=>$lastname,
          'firstname'=>$firstname,
          'middlename'=>$middlename,
          'zone'=>$zone,
          'bday'=>$bday,
          'age'=>$age,
          'servicegive'=>$servicegive,
          'lmp'=>$lmp,
          'edc'=>$edc,
          'gpa'=>$gpa,
          'remarks'=>$remarks,
          'teenagepregnancy'=>$teenagepregnancy,
     
         
        );
      }
      $inserdata=$this->m->insert_batch_teen($data);
      if($inserdata)
      {
        $this->session->set_flashdata('message','<div class="alert alert-success">Successfully Added.</div>');
        redirect('dashboard/importteen');
      } else {
        $this->session->set_flashdata('message','<div class="alert alert-danger">Data Not uploaded. Please Try Again.</div>');
        redirect('dashboard/importteen');
      }
    }
  }
4ioopgfo

4ioopgfo1#

只需将其放在IF语句中。

$lmp=date('y-m-d',strtotime(''));

当strtetime为空时,它始终返回70-01-01。
更改为:

$lmp=$sheetdata[$i][8] ? date('y-m-d',strtotime($sheetdata[$i][8])) : '';

相关问题