From 1c1b3ed5c9413055170337cc76ac7043ceabd484 Mon Sep 17 00:00:00 2001 From: Oded Arbel Date: Sat, 5 Sep 2020 11:47:33 +0300 Subject: [PATCH] Add support for writing date cells --- .../Writer/ODS/Manager/WorksheetManager.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 4dfe9c8..0f8a5ae 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -204,6 +204,22 @@ class WorksheetManager implements WorksheetManagerInterface $data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cell->getValue() . '">'; $data .= '' . $cell->getValue() . ''; $data .= ''; + } elseif ($cell->isDate()) { + $value = $cell->getValue(); + if ($value instanceof \DateTime) { + $data .= ' office:value-type="date" calcext:value-type="date" office:date-value="' . $value->format(\DateTimeInterface::W3C) . '">'; + $data .= '' . $value->format(\DateTimeInterface::W3C) . ''; + } else if ($value instanceof \DateInterval) { + // workaround for missing DateInterval::format('c'), see https://stackoverflow.com/a/61088115/53538 + static $f = ['M0S', 'H0M', 'DT0H', 'M0D', 'Y0M', 'P0Y', 'Y0M', 'P0M']; + static $r = ['M', 'H', 'DT', 'M', 'Y0M', 'P', 'Y', 'P']; + $value = rtrim(str_replace($f, $r, $value->format('P%yY%mM%dDT%hH%iM%sS')), 'PT') ?: $default; + $data .= ' office:value-type="time" office:time-value="' . $value . '">'; + $data .= '' . $value . ''; + } else { + throw new InvalidArgumentException('Trying to add a date value with an unsupported type: ' . \gettype($cell->getValue())); + } + $data .= ''; } elseif ($cell->isError() && is_string($cell->getValueEvenIfError())) { // only writes the error value if it's a string $data .= ' office:value-type="string" calcext:value-type="error" office:value="">';