简体   繁体   English

用PHPExcel写负数时间-XLS

[英]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: 我正在做一个接收完整工作表,减少信息量并回答另一个工作表的类,在该工作表中,该工作表的字段中可能存在负时间(-H:i),该函数是:

$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' . $ hdE = '23: 39' ,则应返回'-0:21'

How can I do this ? 我怎样才能做到这一点 ?

Make PHPexcel return a negative time without calculating from 00:00 使PHPexcel返回负时间而不从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_Style_NumberFormat

PHPExcel v1.6 PHPExcel_Style_NumberFormat::toFormattedString function does, is call sprintf([predefinedFormat], $value) , or returns the results of date() . PHPExcel v1.6 PHPExcel_Style_NumberFormat::toFormattedString函数可以调用sprintf([predefinedFormat], $value) PHPExcel_Style_NumberFormat::toFormattedString sprintf([predefinedFormat], $value)或返回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. 此外,您提供的-HH:i格式参数不是该函数的可接受列表,因此它仅返回值。

PHPExcel v1.8 does allow for highly complex custom number formatting options, but still does not handle negatives in dates or times. PHPExcel v1.8确实允许使用非常复杂的自定义数字格式设置选项,但仍不能处理日期或时间中的负数。

See: PHPExcel github for v1.8 and cmsws phpexcel docs for v1.6 请参阅: 适用于v1.8的PHPExcel github适用于v1.6的cmsws phpexcel文档

Just format it yourself 自己格式化

Note, you can't just call date as that function does, as php date() does not support negative timestamps. 请注意,您不能像该函数那样仅调用date ,因为php date()不支持负时间戳。 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). 尽管从技术上来说,这确实意味着您正在使用1970年1月1日的纪元时间戳,但时间仍然准确(这就是Excel的工作方式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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