Skip to content

Instantly share code, notes, and snippets.

@filaruina
Last active December 19, 2015 15:48
Show Gist options
  • Save filaruina/5978619 to your computer and use it in GitHub Desktop.
Save filaruina/5978619 to your computer and use it in GitHub Desktop.
A simple function to find work days in a time period, using DateTime, DateInterval and DatePeriod. Some refactoring tips are appreciated =)
<?php
function findWorkDays($dateStart, $dateEnd)
{
$oneWeek = new \DateInterval('P7D');
$date1 = new \DateTime($dateStart);
$date2 = new \DateTime($dateEnd);
$interval = $date1->diff($date2);
$freeDays = 0;
//Finding saturdays
$satObj = clone $date1;
$saturdays = new \DatePeriod($satObj->modify('saturday'), $oneWeek, $date2);
foreach ($saturdays as $saturday) {
$freeDays++;
}
//Finding sundays
$sunObj = clone $date1;
$sundays = new \DatePeriod($sunObj->modify('sunday'), $oneWeek, $date2);
foreach ($sundays as $sunday) {
$freeDays++;
}
return $interval->days - $freeDays + 1;
}
@filaruina
Copy link
Author

Currently it doesn't consider holydays

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment