/
dates_and_times03.php
64 lines (59 loc) · 1.88 KB
/
dates_and_times03.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* @see http://libxlsxwriter.github.io/dates_and_times03_8c-example.html
* @noinspection PhpUnhandledExceptionInspection
*/
use FFILibXlsxWriter\FFILibXlsxWriter;
use FFILibXlsxWriter\Enums\Align;
use FFILibXlsxWriter\Structs\DateTime as XlsxDateTime;
use FFILibXlsxWriter\Workbook;
require_once __DIR__ . '/../vendor/autoload.php';
FFILibXlsxWriter::init();
/* A datetime to display. */
$datetime = new XlsxDateTime(2013, 1, 23, 12, 30, 5.123);
$row = 0;
$col = 0;
$i = 0;
/* Examples date and time formats. In the output file compare how changing
* the format strings changes the appearance of the date.
*/
$date_formats = [
"dd/mm/yy",
"mm/dd/yy",
"dd m yy",
"d mm yy",
"d mmm yy",
"d mmmm yy",
"d mmmm yyy",
"d mmmm yyyy",
"dd/mm/yy hh:mm",
"dd/mm/yy hh:mm:ss",
"dd/mm/yy hh:mm:ss.000",
"hh:mm",
"hh:mm:ss",
"hh:mm:ss.000",
];
/* Create a new workbook and add a worksheet. */
$workbook = new Workbook(__DIR__ . '/output/dates_and_times03.xlsx');
$worksheet = $workbook->addWorksheet();
/* Add a bold format. */
$bold = $workbook->addFormat();
$bold->setBold();
/* Write the column headers. */
$worksheet->writeString([$row, $col], "Formatted date", $bold);
$worksheet->writeString([$row, $col + 1], "Format", $bold);
/* Widen the first column to make the text clearer. */
$worksheet->setColumn([0, 1], 20, null);
/* Write the same date and time using each of the above formats. */
for ($i = 0; $i < 14; $i++) {
$row++;
/* Create a format for the date or time.*/
$format = $workbook->addFormat();
$format->setNumFormat($date_formats[$i]);
$format->setAlign(Align::LEFT);
/* Write the datetime with each format. */
$worksheet->writeDatetime([$row, $col], $datetime, $format);
/* Also write the format string for comparison. */
$worksheet->writeString([$row, $col + 1], $date_formats[$i], null);
}
$workbook->close();