gpt4 book ai didi

PhpSpreadsheet 格式化从 XLSX 导入的日期

转载 作者:行者123 更新时间:2023-12-05 08:20:28 31 4
gpt4 key购买 nike

美好的一天。我使用 PhpSpreadsheet 的库来导入 xlsx。

在 xlsx 的 I 列中,我有这个值 20/10/1970 00:00:00 显示为 1970-10-20 00:00:00 但是当我导入它时:

$dataArray = $spreadsheet->getActiveSheet()
->rangeToArray(
'A2:AO3', // The worksheet range that we want to retrieve
NULL, // Value that should be returned for empty cells
NULL, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
TRUE, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
TRUE // Should the array be indexed by cell row and cell column
);

我收到奇怪的 ["I"] => float(25861) 请帮助我获取此字段中的 Carbon 数据。

如果我尝试像时间戳一样转换此 float ,我会收到 01/01/1970 而不是 10/20/1970

我收到的其他日期如 ["J"] => float(43195.4092014)

最佳答案

Excel 中的日期与其他软件/编程语言中的日期不同。

Excel 从 1900-01-01 开始计数,与 UNIX 从 1970-01-01 开始计数不同。
返回的数字是从 1900-01-01 开始的天数的浮点值, float 的小数部分是一天中的时间。

例如 25861.5 是 1970-10-20 12:00(“Y-m-d h.i”)。

要转换为 UNIX,您应该使用返回值 - 25569(Excel 中的 1970-01-01)并乘以 86400(以秒为单位的一天)。

看这里: https://3v4l.org/AudMn

在 Excel 中,如果将 25861 和 25569 格式化为日期:
enter image description here

你得到的另一个 float 43195.4092013889

echo date("Y-m-d h:i", (43195.4092013889-25569)*86400); //2018-04-05 11:49 (mind the timezones)

https://3v4l.org/mMZQ4

关于PhpSpreadsheet 格式化从 XLSX 导入的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50366682/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com