PHPSpreadsheet:为时间格式化单元格还包括日期
PHPSpreadsheet: Formatting a cell for time also includes the date
我正在尝试使用 PHPSpreadsheet 格式化时间单元格,但在查看公式栏时它似乎也包括日期。从字符串、日期时间对象或 unix 时间戳转换时似乎也存在一些不一致。
<?php
include '../vendor/autoload.php';
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$times = [
'16:00:00',
new \DateTime('16:00:00'),
\strtotime('16:00:00'),
'2020-04-04 16:00:00',
new \DateTime('2020-02-04 16:00:00'),
\strtotime('2020-02-04 16:00:00'),
];
$format = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME1;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
foreach ($times as $i => $time) {
$sheet->setCellValue('A' . ($i+1), \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($time));
$sheet->getStyle('A' . ($i+1))->getNumberFormat()->setFormatCode($format);
}
$writer = new Xlsx($spreadsheet);
$writer->save('test.xlsx');
来自 \PHPOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME1
:const FORMAT_DATE_TIME1 = 'h:mm AM/PM';
这是错误还是预期的功能?考虑到 const FORMAT_DATE_DATETIME = 'd/m/yy h:mm';
确实 包含一些日期参数,我认为发生了一些错误。
下面是一些截图:
但是如果我们在单元格中输入“5:00 AM”,公式栏不包括日期:
这是右键单击 > "Format Cell" 弹出的屏幕:
有人可以告诉我我做错了什么吗,谢谢。
您必须设置正确的数字格式,有关详细信息,请参阅 official sample documentation。
您设置的数字格式是'h:mm AM/PM'
表示没有日期只包含时间。
当您更改为下午 5 点时,您将覆盖内容并使用数字格式。
如果你有约会,你应该尝试设置 'd/m/yy h:mm AM/PM'
.
我想出了解决这个问题的办法:你需要计算时间戳的 Excel 表示,然后只得到小数点后的数字。
<?php
$timestamp = new \DateTime('16:00:00');
$excelTimestamp = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($timestamp);
$excelDate = floor($excelTimestamp);
$time = $excelTimestamp - $excelDate;
来自文档:
In Excel, dates and Times are stored as numeric values counting the number of days elapsed since 1900-01-01. For example, the date '2008-12-31' is represented as 39813. You can verify this in Microsoft Office Excel by entering that date in a cell and afterwards changing the number format to 'General' so the true numeric value is revealed. Likewise, '3:15 AM' is represented as 0.135417.
我希望这可以帮助其他遇到此问题的人。
我正在尝试使用 PHPSpreadsheet 格式化时间单元格,但在查看公式栏时它似乎也包括日期。从字符串、日期时间对象或 unix 时间戳转换时似乎也存在一些不一致。
<?php
include '../vendor/autoload.php';
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$times = [
'16:00:00',
new \DateTime('16:00:00'),
\strtotime('16:00:00'),
'2020-04-04 16:00:00',
new \DateTime('2020-02-04 16:00:00'),
\strtotime('2020-02-04 16:00:00'),
];
$format = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME1;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
foreach ($times as $i => $time) {
$sheet->setCellValue('A' . ($i+1), \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($time));
$sheet->getStyle('A' . ($i+1))->getNumberFormat()->setFormatCode($format);
}
$writer = new Xlsx($spreadsheet);
$writer->save('test.xlsx');
来自 \PHPOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME1
:const FORMAT_DATE_TIME1 = 'h:mm AM/PM';
这是错误还是预期的功能?考虑到 const FORMAT_DATE_DATETIME = 'd/m/yy h:mm';
确实 包含一些日期参数,我认为发生了一些错误。
下面是一些截图:
但是如果我们在单元格中输入“5:00 AM”,公式栏不包括日期:
这是右键单击 > "Format Cell" 弹出的屏幕:
有人可以告诉我我做错了什么吗,谢谢。
您必须设置正确的数字格式,有关详细信息,请参阅 official sample documentation。
您设置的数字格式是'h:mm AM/PM'
表示没有日期只包含时间。
当您更改为下午 5 点时,您将覆盖内容并使用数字格式。
如果你有约会,你应该尝试设置 'd/m/yy h:mm AM/PM'
.
我想出了解决这个问题的办法:你需要计算时间戳的 Excel 表示,然后只得到小数点后的数字。
<?php
$timestamp = new \DateTime('16:00:00');
$excelTimestamp = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($timestamp);
$excelDate = floor($excelTimestamp);
$time = $excelTimestamp - $excelDate;
来自文档:
In Excel, dates and Times are stored as numeric values counting the number of days elapsed since 1900-01-01. For example, the date '2008-12-31' is represented as 39813. You can verify this in Microsoft Office Excel by entering that date in a cell and afterwards changing the number format to 'General' so the true numeric value is revealed. Likewise, '3:15 AM' is represented as 0.135417.
我希望这可以帮助其他遇到此问题的人。