- Impact
- 0
Hi there! I have a tricky problem finding the number of days until a particular date using only its creation date. The only date held in the DB is its creation date and the day of the week it recurs on. The Day of the week is represented as 0-7. I use timestamps to find the exact number of seconds between the two dates (eventStartDate is a timestamp) and then divide them by 7 to find the number of weeks then I round up the result to get an even number of weeks. The function is supposed to return the number of days until the next Repeating event which I can append onto a time stamp to show the exact date.
Any help solving or make the function more efficient would be appreciated. Thanks
Code:
function nextRepEvent($currentDayNo, $eventRecurDay, $eventStartDate){
//Easier to work with "7" representing Sunday
if ($eventRecurDay==0){
$eventRecurDay=7;
}
if($currentDayNo > $eventRecurDay)
{
if ($eventStartDate - mktime(0, 0, 0, date("m"), date("d"), date("Y")) < 1)
{
//event is in the following week
$diff = 7 - $currentDayNo + $eventRecurDay;
}else{
//event is more than one week in the future
$weeksUntil = ceil( 7 / ($eventStartDate - mktime(0, 0, 0, date("m"), date("d"), date("Y"))));
$diff = $weeksUntil*7 - $currentDayNo + $eventRecurDay;
}
}else
{
//event is within the current week
$diff = $eventRecurDay - $this->currentDayNo;
}
return $diff;
}
Any help solving or make the function more efficient would be appreciated. Thanks




