简体   繁体   中英

Write a negative time with PHPExcel - XLS

I'm doing a class that receives a complete worksheet, reduces the amount of information, and answers another worksheet, in that worksheet that has a field where there can be negative time (-H: i), the function:

$file ['AH42'] = '-0:21';

$hdE = \PHPExcel_Style_NumberFormat::toFormattedString($file['AH42'], 'HH:i'));

Give me back:

$ hdE = '23: 39' , when it should return '-0:21' .

How can I do this ?

Make PHPexcel return a negative time without calculating from 00:00

Maybe move minus sign to format string?

$file ['AH42'] = '0:21'
$hdE = \PHPExcel_Style_NumberFormat::toFormattedString($file['AH42'], '-HH:i'));

Don't use PHPExcel_Style_NumberFormat

PHPExcel v1.6 PHPExcel_Style_NumberFormat::toFormattedString function does, is call sprintf([predefinedFormat], $value) , or returns the results of date() . None of the predefined formats can handle negative time. Further, the format argument you gave -HH:i isn't that function's accepted list, so it just returns the value.

PHPExcel v1.8 does allow for highly complex custom number formatting options, but still does not handle negatives in dates or times.

See: PHPExcel github for v1.8 and cmsws phpexcel docs for v1.6

Just format it yourself

Note, you can't just call date as that function does, as php date() does not support negative timestamps. So you'll have to manually account for the sign.

$time = $file['AH42'];
$hdE = ($time < 0) ? '-' : '';
$hdE = date('H:i', (1 * abs($time)));

While this does technically mean you're operating on an Epoch timestamp on 1 Jan 1970, the time will still be accurate (that's how Excel works).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM