Skip to content

Instantly share code, notes, and snippets.

@cat5inthecradle
Last active April 12, 2016 16:44
Show Gist options
  • Select an option

  • Save cat5inthecradle/935b8ed31dfedcc253351ed1e0332e90 to your computer and use it in GitHub Desktop.

Select an option

Save cat5inthecradle/935b8ed31dfedcc253351ed1e0332e90 to your computer and use it in GitHub Desktop.
Calculates the end time given a start time a number of hours remaining
function getEndTime(DateTime $startDate, int $hoursRemaining) {
$endDate = $startDate;
# Can we finish it today?
$hoursLeftInWorkday = $this->getHoursLeftInWorkday($startDate);
if ($hoursRemaining < $hoursLeftInWorkday) {
$endDate->modify('+' . $hoursRemaining . ' hours');
return $endDate;
}
# How many full days after the rest of today are needed?
$hoursRemainingAdjusted = $hoursRemaining - $hoursLeftInWorkday;
$endDate = $endDate->modify('+1 day');
# Can we finish on the current $endDate?
while ($hoursRemainingAdjusted - 8 >= 0 ) {
# skip the weekends
while ($endDate != WORKDAY) {
$endDate->modify('+1 day');
}
# use up a workday
$hoursRemainingAdjusted = $hoursRemainingAdjusted - 8;
$endDate->modify('+1 day');
}
# How many partial day hours are needed after that
$endDateHour = WORKDAY_START_HOUR + $hoursRemainingAdjusted;
$endDate = $endDate->setTime($endDateHour, 00)
return $endDate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment