Created
August 21, 2015 20:50
-
-
Save jasonbradley/9ead3908e57941c79b2f to your computer and use it in GitHub Desktop.
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
<?php | |
function isNextDateGreaterThanHalf($frequency, $seasonalEndDate, $nextDate) | |
{ | |
$dateDiff = (strtotime($seasonalEndDate) > strtotime($nextDate)) | |
? strtotime($seasonalEndDate) - strtotime($nextDate) | |
: strtotime($nextDate) - strtotime($seasonalEndDate); | |
$daysDiff = floor($dateDiff / (60*60*24)); //seconds * mins in hour * hours in day | |
return ($daysDiff > ($frequency / 2)); | |
} | |
function addDays($date, $days) | |
{ | |
$strToTimeAddDays = date('Y-m-d', strtotime($date)) . ' + ' . $days . ' Days'; | |
$date = date('Y-m-d', strtotime($strToTimeAddDays)); | |
return $date; | |
} | |
function addYears($date, $years) | |
{ | |
$strToTimeAddDays = date('Y-m-d', strtotime($date)) . ' + ' . $years . ' Years'; | |
$date = date('Y-m-d', strtotime($strToTimeAddDays)); | |
return $date; | |
} | |
function getUpComingServices($lastServiceDate, $seasonalStartDate, $seasonalEndDate, $frequency, $numberOfServices) | |
{ | |
echo "Every $frequency days for $numberOfServices services.<br/><br/>"; | |
echo "Last Service Date: $lastServiceDate <br/><br/>"; | |
echo "Start of Season Date: $seasonalStartDate <br/>"; | |
echo "End of Season Date: $seasonalEndDate <br/>"; | |
//list of dates to service customer | |
$scheduledDates = []; | |
//date before start of season | |
if (strtotime($lastServiceDate) < strtotime($seasonalStartDate)) { | |
$lastServiceDate = $seasonalStartDate; | |
} | |
//for each number of services deterimine the next service date | |
for ($i = 0; $i < $numberOfServices; $i++) { | |
$nextDate = (!isset($nextDate)) ? $lastServiceDate : addDays($nextDate, $frequency); | |
//date after end of season | |
if (strtotime($nextDate) > strtotime($seasonalEndDate)) { | |
$isPastHalf = isNextDateGreaterThanHalf($frequency, | |
$seasonalEndDate, | |
$nextDate); | |
//reset for next season | |
if ($isPastHalf === true) { | |
$seasonalStartDate = addYears($seasonalStartDate, 1); | |
$seasonalEndDate = addYears($seasonalEndDate, 1); | |
$nextDate = $seasonalStartDate; | |
} | |
} | |
//add the date to the list | |
$scheduledDates[] = date("Y-m-d", strtotime($nextDate)); | |
} | |
return $scheduledDates; | |
} | |
$lastServiceDate = date("2015-11-10"); | |
$frequency = 30; | |
$seasonalStartDate = date("2015-03-01"); | |
$seasonalEndDate = date("2015-10-31"); | |
$numberOfServices = 25; | |
$scheduledDates = getUpComingServices($lastServiceDate, | |
$seasonalStartDate, | |
$seasonalEndDate, | |
$frequency, | |
$numberOfServices); | |
echo "<pre>"; | |
var_dump($scheduledDates); | |
echo "</pre><br/><br/>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment