If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

# Thread: Find a recurring date based on its creation date

1. 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.

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

2.

3. Out of curiosity, why are you working this all in timestamp-land and not doing it in basis to the day of the year? That way, you would have to deal with one contingency (the 365-0 changeover) and you'd be able to do calculations in a reference frame that makes a lot more sense for your purposes.

4. I went with the timestamps for accuracy to ensure that even across timezones the solution could be deployed the same. But I may switch. Got to leave this one to breathe a while I think.