Skip to content

Commit 099ab78

Browse files
committed
Update Excel function samples for Date/Time functions
1 parent ad2d3df commit 099ab78

File tree

15 files changed

+494
-22
lines changed

15 files changed

+494
-22
lines changed

samples/Calculations/DateTime/DATE.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
require __DIR__ . '/../../Header.php';
66

7-
$helper->log('Returns the serial number of a particular date.');
7+
$category = 'Date/Time';
8+
$functionName = 'DATE';
9+
$description = 'Returns the Excel serial number of a particular date';
10+
11+
$helper->titles($category, $functionName, $description);
812

913
// Create new PhpSpreadsheet object
1014
$spreadsheet = new Spreadsheet();
@@ -27,15 +31,15 @@
2731
}
2832
$worksheet->getStyle('E1:E' . $testDateCount)
2933
->getNumberFormat()
30-
->setFormatCode('yyyy-mmm-dd');
34+
->setFormatCode('yyyy-mm-dd');
3135

3236
// Test the formulae
3337
for ($row = 1; $row <= $testDateCount; ++$row) {
34-
$helper->log('Year: ' . $worksheet->getCell('A' . $row)->getFormattedValue());
35-
$helper->log('Month: ' . $worksheet->getCell('B' . $row)->getFormattedValue());
36-
$helper->log('Day: ' . $worksheet->getCell('C' . $row)->getFormattedValue());
38+
$helper->log("(A{$row}) Year: " . $worksheet->getCell('A' . $row)->getFormattedValue());
39+
$helper->log("(B{$row}) Month: " . $worksheet->getCell('B' . $row)->getFormattedValue());
40+
$helper->log("(C{$row}) Day: " . $worksheet->getCell('C' . $row)->getFormattedValue());
3741
$helper->log('Formula: ' . $worksheet->getCell('D' . $row)->getValue());
38-
$helper->log('Excel DateStamp: ' . $worksheet->getCell('D' . $row)->getFormattedValue());
42+
$helper->log('Excel DateStamp: ' . $worksheet->getCell('D' . $row)->getCalculatedValue());
3943
$helper->log('Formatted DateStamp: ' . $worksheet->getCell('E' . $row)->getFormattedValue());
4044
$helper->log('');
4145
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'DATEDIF';
9+
$description = 'Calculates the number of days, months, or years between two dates';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
// Add some data
18+
$testDates = [
19+
[1900, 1, 1],
20+
[1904, 1, 1],
21+
[1936, 3, 17],
22+
[1960, 12, 19],
23+
[1999, 12, 31],
24+
[2000, 1, 1],
25+
[2019, 2, 14],
26+
[2020, 7, 4],
27+
];
28+
$testDateCount = count($testDates);
29+
30+
$worksheet->fromArray($testDates, null, 'A1', true);
31+
32+
for ($row = 1; $row <= $testDateCount; ++$row) {
33+
$worksheet->setCellValue('D' . $row, '=DATE(A' . $row . ',B' . $row . ',C' . $row . ')');
34+
$worksheet->setCellValue('E' . $row, '=D' . $row);
35+
$worksheet->setCellValue('F' . $row, '=TODAY()');
36+
$worksheet->setCellValue('G' . $row, '=DATEDIF(D' . $row . ', F' . $row . ', "Y")');
37+
$worksheet->setCellValue('H' . $row, '=DATEDIF(D' . $row . ', F' . $row . ', "M")');
38+
$worksheet->setCellValue('I' . $row, '=DATEDIF(D' . $row . ', F' . $row . ', "D")');
39+
$worksheet->setCellValue('J' . $row, '=DATEDIF(D' . $row . ', F' . $row . ', "MD")');
40+
$worksheet->setCellValue('K' . $row, '=DATEDIF(D' . $row . ', F' . $row . ', "YM")');
41+
$worksheet->setCellValue('L' . $row, '=DATEDIF(D' . $row . ', F' . $row . ', "YD")');
42+
}
43+
$worksheet->getStyle('E1:F' . $testDateCount)
44+
->getNumberFormat()
45+
->setFormatCode('yyyy-mm-dd');
46+
47+
// Test the formulae
48+
for ($row = 1; $row <= $testDateCount; ++$row) {
49+
$helper->log(sprintf(
50+
'Between: %s and %s',
51+
$worksheet->getCell('E' . $row)->getFormattedValue(),
52+
$worksheet->getCell('F' . $row)->getFormattedValue()
53+
));
54+
$helper->log('In years ("Y"): ' . $worksheet->getCell('G' . $row)->getCalculatedValue());
55+
$helper->log('In months ("M"): ' . $worksheet->getCell('H' . $row)->getCalculatedValue());
56+
$helper->log('In days ("D"): ' . $worksheet->getCell('I' . $row)->getCalculatedValue());
57+
$helper->log('In days ignoring months and years ("MD"): ' . $worksheet->getCell('J' . $row)->getCalculatedValue());
58+
$helper->log('In months ignoring days and years ("YM"): ' . $worksheet->getCell('K' . $row)->getCalculatedValue());
59+
$helper->log('In days ignoring years ("YD"): ' . $worksheet->getCell('L' . $row)->getCalculatedValue());
60+
}

samples/Calculations/DateTime/DATEVALUE.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
require __DIR__ . '/../../Header.php';
66

7-
$helper->log('Converts a date in the form of text to a serial number.');
7+
$category = 'Date/Time';
8+
$functionName = 'DATEVALUE';
9+
$description = 'Converts a date in the form of text to an Excel serial number';
10+
11+
$helper->titles($category, $functionName, $description);
812

913
// Create new PhpSpreadsheet object
1014
$spreadsheet = new Spreadsheet();
@@ -13,8 +17,8 @@
1317
// Add some data
1418
$testDates = ['26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012',
1519
'2012-Oct-31', '5th November', 'January 1st', 'April 2012',
16-
'17-03', '03-2012', '29 Feb 2011', '03-05-07',
17-
'03-MAY-07', '03-13-07',
20+
'17-03', '03-17', '03-2012', '29 Feb 2011', '03-05-07',
21+
'03-MAY-07', '03-13-07', '13-03-07', '03/13/07', '13/03/07',
1822
];
1923
$testDateCount = count($testDates);
2024

@@ -26,14 +30,14 @@
2630

2731
$worksheet->getStyle('C1:C' . $testDateCount)
2832
->getNumberFormat()
29-
->setFormatCode('yyyy-mmm-dd');
33+
->setFormatCode('yyyy-mm-dd');
3034

3135
// Test the formulae
3236
$helper->log('<strong>Warning: </strong>The PhpSpreadsheet DATEVALUE() function accepts a wider range of date formats than MS Excel DATEFORMAT() function.');
3337
for ($row = 1; $row <= $testDateCount; ++$row) {
34-
$helper->log('Date String: ' . $worksheet->getCell('A' . $row)->getFormattedValue());
38+
$helper->log("(A{$row}) Date String: " . $worksheet->getCell('A' . $row)->getFormattedValue());
3539
$helper->log('Formula: ' . $worksheet->getCell('B' . $row)->getValue());
36-
$helper->log('Excel DateStamp: ' . $worksheet->getCell('B' . $row)->getFormattedValue());
37-
$helper->log('Formatted DateStamp' . $worksheet->getCell('C' . $row)->getFormattedValue());
40+
$helper->log('Excel DateStamp: ' . $worksheet->getCell('B' . $row)->getCalculatedValue());
41+
$helper->log('Formatted DateStamp: ' . $worksheet->getCell('C' . $row)->getFormattedValue());
3842
$helper->log('');
3943
}

samples/Calculations/DateTime/DAY.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'DAY';
9+
$description = 'Returns the day of a date, an integer ranging from 1 to 31';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
// Add some data
18+
$testDates = [
19+
[1900, 1, 1],
20+
[1904, 2, 14],
21+
[1936, 3, 17],
22+
[1964, 4, 29],
23+
[1999, 5, 18],
24+
[2000, 6, 21],
25+
[2019, 7, 4],
26+
[2020, 8, 31],
27+
[1956, 9, 10],
28+
[2010, 10, 10],
29+
[1982, 11, 30],
30+
[1960, 12, 19],
31+
];
32+
$testDateCount = count($testDates);
33+
34+
$worksheet->fromArray($testDates, null, 'A1', true);
35+
36+
for ($row = 1; $row <= $testDateCount; ++$row) {
37+
$worksheet->setCellValue('D' . $row, '=DATE(A' . $row . ',B' . $row . ',C' . $row . ')');
38+
$worksheet->setCellValue('E' . $row, '=D' . $row);
39+
$worksheet->setCellValue('F' . $row, '=DAY(D' . $row . ')');
40+
}
41+
$worksheet->getStyle('E1:E' . $testDateCount)
42+
->getNumberFormat()
43+
->setFormatCode('yyyy-mm-dd');
44+
45+
// Test the formulae
46+
for ($row = 1; $row <= $testDateCount; ++$row) {
47+
$helper->log(sprintf('(E%d): %s', $row, $worksheet->getCell('E' . $row)->getFormattedValue()));
48+
$helper->log('Day is: ' . $worksheet->getCell('F' . $row)->getCalculatedValue());
49+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'DAYS';
9+
$description = 'Returns the number of days between two dates';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
// Add some data
18+
$testDates = [
19+
[1900, 1, 1],
20+
[1904, 1, 1],
21+
[1936, 3, 17],
22+
[1960, 12, 19],
23+
[1999, 12, 31],
24+
[2000, 1, 1],
25+
[2019, 2, 14],
26+
[2020, 7, 4],
27+
[2029, 12, 31],
28+
[2525, 1, 1],
29+
];
30+
$testDateCount = count($testDates);
31+
32+
$worksheet->fromArray($testDates, null, 'A1', true);
33+
34+
for ($row = 1; $row <= $testDateCount; ++$row) {
35+
$worksheet->setCellValue('D' . $row, '=DATE(A' . $row . ',B' . $row . ',C' . $row . ')');
36+
$worksheet->setCellValue('E' . $row, '=D' . $row);
37+
$worksheet->setCellValue('F' . $row, '=TODAY()');
38+
$worksheet->setCellValue('G' . $row, '=DAYS(D' . $row . ', F' . $row . ')');
39+
}
40+
$worksheet->getStyle('E1:F' . $testDateCount)
41+
->getNumberFormat()
42+
->setFormatCode('yyyy-mm-dd');
43+
44+
// Test the formulae
45+
for ($row = 1; $row <= $testDateCount; ++$row) {
46+
$helper->log(sprintf(
47+
'Between: %s and %s',
48+
$worksheet->getCell('E' . $row)->getFormattedValue(),
49+
$worksheet->getCell('F' . $row)->getFormattedValue()
50+
));
51+
$helper->log('Days: ' . $worksheet->getCell('G' . $row)->getCalculatedValue());
52+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'DAYS360';
9+
$description = 'Returns the number of days between two dates based on a 360-day year';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
// Add some data
18+
$testDates = [
19+
[1900, 1, 1],
20+
[1904, 1, 1],
21+
[1936, 3, 17],
22+
[1960, 12, 19],
23+
[1999, 12, 31],
24+
[2000, 1, 1],
25+
[2019, 2, 14],
26+
[2020, 7, 4],
27+
[2029, 12, 31],
28+
[2525, 1, 1],
29+
];
30+
$testDateCount = count($testDates);
31+
32+
$worksheet->fromArray($testDates, null, 'A1', true);
33+
34+
for ($row = 1; $row <= $testDateCount; ++$row) {
35+
$worksheet->setCellValue('D' . $row, '=DATE(A' . $row . ',B' . $row . ',C' . $row . ')');
36+
$worksheet->setCellValue('E' . $row, '=D' . $row);
37+
$worksheet->setCellValue('F' . $row, '=DATE(2022,12,31)');
38+
$worksheet->setCellValue('G' . $row, '=DAYS360(D' . $row . ', F' . $row . ', FALSE)');
39+
$worksheet->setCellValue('H' . $row, '=DAYS360(D' . $row . ', F' . $row . ', TRUE)');
40+
}
41+
$worksheet->getStyle('E1:F' . $testDateCount)
42+
->getNumberFormat()
43+
->setFormatCode('yyyy-mm-dd');
44+
45+
// Test the formulae
46+
for ($row = 1; $row <= $testDateCount; ++$row) {
47+
$helper->log(sprintf(
48+
'Between: %s and %s',
49+
$worksheet->getCell('E' . $row)->getFormattedValue(),
50+
$worksheet->getCell('F' . $row)->getFormattedValue()
51+
));
52+
$helper->log(sprintf(
53+
'Days: %d (US) %d (European)',
54+
$worksheet->getCell('G' . $row)->getCalculatedValue(),
55+
$worksheet->getCell('H' . $row)->getCalculatedValue()
56+
));
57+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'EDATE';
9+
$description = 'Returns the serial number that represents the date that is the indicated number of months before or after a specified date';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
$months = range(-12, 12);
18+
$testDateCount = count($months);
19+
20+
for ($row = 1; $row <= $testDateCount; ++$row) {
21+
$worksheet->setCellValue('A' . $row, '=DATE(2020,12,31)');
22+
$worksheet->setCellValue('B' . $row, '=A' . $row);
23+
$worksheet->setCellValue('C' . $row, $months[$row - 1]);
24+
$worksheet->setCellValue('D' . $row, '=EDATE(B' . $row . ', C' . $row . ')');
25+
}
26+
$worksheet->getStyle('B1:B' . $testDateCount)
27+
->getNumberFormat()
28+
->setFormatCode('yyyy-mm-dd');
29+
30+
$worksheet->getStyle('D1:D' . $testDateCount)
31+
->getNumberFormat()
32+
->setFormatCode('yyyy-mm-dd');
33+
34+
// Test the formulae
35+
for ($row = 1; $row <= $testDateCount; ++$row) {
36+
$helper->log(sprintf(
37+
'%s and %d months is %d (%s)',
38+
$worksheet->getCell('B' . $row)->getFormattedValue(),
39+
$worksheet->getCell('C' . $row)->getFormattedValue(),
40+
$worksheet->getCell('D' . $row)->getCalculatedValue(),
41+
$worksheet->getCell('D' . $row)->getFormattedValue()
42+
));
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'EOMONTH';
9+
$description = 'Returns the serial number for the last day of the month that is the indicated number of months before or after start_date';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
$months = range(-12, 12);
18+
$testDateCount = count($months);
19+
20+
for ($row = 1; $row <= $testDateCount; ++$row) {
21+
$worksheet->setCellValue('A' . $row, '=DATE(2020,1,1)');
22+
$worksheet->setCellValue('B' . $row, '=A' . $row);
23+
$worksheet->setCellValue('C' . $row, $months[$row - 1]);
24+
$worksheet->setCellValue('D' . $row, '=EOMONTH(B' . $row . ', C' . $row . ')');
25+
}
26+
$worksheet->getStyle('B1:B' . $testDateCount)
27+
->getNumberFormat()
28+
->setFormatCode('yyyy-mm-dd');
29+
30+
$worksheet->getStyle('D1:D' . $testDateCount)
31+
->getNumberFormat()
32+
->setFormatCode('yyyy-mm-dd');
33+
34+
// Test the formulae
35+
for ($row = 1; $row <= $testDateCount; ++$row) {
36+
$helper->log(sprintf(
37+
'%s and %d months is %d (%s)',
38+
$worksheet->getCell('B' . $row)->getFormattedValue(),
39+
$worksheet->getCell('C' . $row)->getFormattedValue(),
40+
$worksheet->getCell('D' . $row)->getCalculatedValue(),
41+
$worksheet->getCell('D' . $row)->getFormattedValue()
42+
));
43+
}

0 commit comments

Comments
 (0)