Last active
April 12, 2016 16:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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