WORKDAY.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../../Header.php';
  4. $category = 'Date/Time';
  5. $functionName = 'WORKDAY';
  6. $description = 'Returns a number that represents a date that is the indicated number of working days before or after a starting date. Working days exclude weekends and any dates identified as holidays';
  7. $helper->titles($category, $functionName, $description);
  8. // Create new PhpSpreadsheet object
  9. $spreadsheet = new Spreadsheet();
  10. $worksheet = $spreadsheet->getActiveSheet();
  11. // Add some data
  12. $publicHolidays = [
  13. [2022, 1, 3, '=DATE(G1, H1, I1)', 'New Year'],
  14. [2022, 4, 15, '=DATE(G2, H2, I2)', 'Good Friday'],
  15. [2022, 4, 18, '=DATE(G3, H3, I3)', 'Easter Monday'],
  16. [2022, 5, 2, '=DATE(G4, H4, I4)', 'Early May Bank Holiday'],
  17. [2022, 6, 2, '=DATE(G5, H5, I5)', 'Spring Bank Holiday'],
  18. [2022, 6, 3, '=DATE(G6, H6, I6)', 'Platinum Jubilee Bank Holiday'],
  19. [2022, 8, 29, '=DATE(G7, H7, I7)', 'Summer Bank Holiday'],
  20. [2022, 12, 26, '=DATE(G8, H8, I8)', 'Boxing Day'],
  21. [2022, 12, 27, '=DATE(G9, H9, I9)', 'Christmas Day'],
  22. ];
  23. $holidayCount = count($publicHolidays);
  24. $worksheet->fromArray($publicHolidays, null, 'G1', true);
  25. $worksheet->getStyle('J1:J' . $holidayCount)
  26. ->getNumberFormat()
  27. ->setFormatCode('yyyy-mm-dd');
  28. $worksheet->setCellValue('A1', '=DATE(2022,1,1)');
  29. $workdayStep = 10;
  30. for ($days = $workdayStep; $days <= 366; $days += $workdayStep) {
  31. $worksheet->setCellValue('B' . ((int) $days / $workdayStep + 1), $days);
  32. $worksheet->setCellValue('C' . ((int) $days / $workdayStep + 1), '=WORKDAY(A1, B' . ((int) $days / $workdayStep + 1) . ')');
  33. $worksheet->setCellValue('D' . ((int) $days / $workdayStep + 1), '=WORKDAY(A1, B' . ((int) $days / $workdayStep + 1) . ', J1:J' . $holidayCount . ')');
  34. }
  35. $worksheet->getStyle('A1')
  36. ->getNumberFormat()
  37. ->setFormatCode('yyyy-mm-dd');
  38. $worksheet->getStyle('C1:D50')
  39. ->getNumberFormat()
  40. ->setFormatCode('yyyy-mm-dd');
  41. // Test the formulae
  42. $helper->log('UK Public Holidays');
  43. $holidayData = $worksheet->rangeToArray('J1:K' . $holidayCount, null, true, true, true);
  44. $helper->displayGrid($holidayData);
  45. for ($days = $workdayStep; $days <= 366; $days += $workdayStep) {
  46. $helper->log(sprintf(
  47. '%d workdays from %s is %s; %s with public holidays',
  48. $worksheet->getCell('B' . ((int) $days / $workdayStep + 1))->getFormattedValue(),
  49. $worksheet->getCell('A1')->getFormattedValue(),
  50. $worksheet->getCell('C' . ((int) $days / $workdayStep + 1))->getFormattedValue(),
  51. $worksheet->getCell('D' . ((int) $days / $workdayStep + 1))->getFormattedValue()
  52. ));
  53. }