dtx9931 2015-06-19 07:04
浏览 141

PHPExcel有没有办法以dd-mm-yyyy格式验证输入日期?

I have following code for reading data from .xls file.

$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(false);

$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

$chunksize=_CHUNKSIZE_;
$endRow=$chunksize;
$count=ceil($highestRow/$chunksize);

for ($row=1;$row<=$endRow;$row++)
        {
            if($row>$highestRow){
                break;
            }
            for ($col=0;$col<$highestColumnIndex;$col++)
            {
                $cellobj=$objWorksheet->getCellByColumnAndRow($col, $row);
                $value=$cellobj->getValue();
                if(PHPExcel_Shared_Date::isDateTime($cellobj)) { //to check the date
                    $value=$cellobj->getFormattedValue();
                    
                }
            }
        }

Here it returns the date in 'mm-dd-yyyy' format when in any date is provided in any format. I actually need to validate that the date should be in the 'dd-mm-yyyy'.

</div>
  • 写回答

1条回答 默认 最新

  • douniang3866 2015-06-19 07:13
    关注

    Rather than use

    if(PHPExcel_Shared_Date::isDateTime($cellobj)) { //to check the date
        $value=$cellobj->getFormattedValue();
    }
    

    to verify if a cell contains a date, and convert it to a string formatted using the cells format mask, use

    if(PHPExcel_Shared_Date::isDateTime($cellobj)) { //to check the date
        $datetimeobj = PHPExcel_Shared_Date::ExcelToPHPObject($cellobj->getValue());
    }
    

    and you can then use the DateTime object's format() method to format it however you want

    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题