PHPExcel String to Time

若如初见. 提交于 2019-12-11 02:11:33

问题


I have created an Excel spreadsheet. In the second column I have values like 0:11:23 and 2:03:33, several thousand rows worth. Using PHP I set the format to be:

    $sheet->getStyle($colRange)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);

But the cells are all still just strings. If you edit a cell from 0:11:22 to something else like 0:11:33 then the cell WILL get converted into a time. At the top it goes from 0:11:22 to 0:11:33 AM this auto addition of AM is what seems to be the huge change, and then I can do what I would like with the file.

How can I change the thousands of cells from 0:11:22 to 0:11:22 AM ?


回答1:


That's because you have to convert the date/time to an MS Excel serialized timestamp, and store that value in the cell; as described in the PHPExcel Documentation and shown in 02types.php in the /Examples folder.

Once you've stored an actual MS Excel timestamp value, then you can apply a format to it

$objPHPExcel->getActiveSheet()->setCellValue('A12', 'Date/Time')
    ->setCellValue('B12', 'Time')
        ->setCellValue('C12', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objPHPExcel->getActiveSheet()
    ->getStyle('C12')
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);



回答2:


This is an old post but maybe it would help someone...

$objPHPExcel->getActiveSheet()
->setCellValue('E5', '=TEXT(TIMEVALUE("'. $the_str_Time . '"),"[h]:mm")');

The TIMEVALUE function turns the string into time, And the TEXT function gives them a format as desired. Mine was for measuring time for more then 24 hours




回答3:


try this

$sheet
    ->getStyle($colRange)
    ->getNumberFormat()
    ->setFormatCode('[$-C09]d mmm yyyy;@');


来源:https://stackoverflow.com/questions/39565513/phpexcel-string-to-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!