Jump to content

Creating a calendar with reoccuring events


scrubbicus

Recommended Posts

Might anyone know a good tutorial on how I might go about doing this? I found a simple calendar script that works perfectly but now I'm trying make it so I can save an event that has a start and end date that are different then each other for reoccurring to a database then have it display correctly on the calendar when I query the database.

 

I have no clue if I'm even going about this the right way. I haven't even started diving in to if the event was to reoccur once a week or once a month, I'm just trying to get it so it displays on the start date then displays every day until the end date. Right now I take the current day, month, year that the calendar is looping on and check it against the event day, month, year. If the current calendar day is on or after the event start date then it returns true and if the current calendar day is on or before the event end date it returns true.... so then if it returns true on both then I give it the go ahead to display on that day.

 

What I have is:

if($current_date['year'] > $event_date['year']) {

                $result = ($return == 'boolean') ? true : 'trail';

            } else

            if($current_date['year'] == $event_date['year']) {

                if($current_date['month'] > $event_date['month']) {

                    $result = ($return == 'boolean') ? true : 'trail';

                } else

                if($current_date['month'] == $event_date['month']) {

                    if($current_date['day'] > $event_date['day']) {

                        $result = ($return == 'boolean') ? true : 'trail';

                    } else

                    if($current_date['day'] == $event_date['day']) {

                        $result = ($return == 'boolean') ? true : 'today';

                    } else

                    if($current_date['day'] < $event_date['day']) {

                        $result = false;

                    }

                } else

                if($current_date['month'] < $event_date['month']) {

                    $result = false;

                }

            } else

            if($current_date['year'] < $event_date['year']) {

                $result = false;

            }

        break;

       

        case 'end':

            if($current_date['year'] < $event_date['year']) {

                $result = ($return == 'boolean') ? true : 'trail';

            } else

            if($current_date['year'] == $event_date['year']) {

                if($current_date['month'] < $event_date['month']) {

                    $result = ($return == 'boolean') ? true : 'trail';

                } else

                if($current_date['month'] == $event_date['month']) {

                    if($current_date['day'] < $event_date['day']) {

                    $result = ($return == 'boolean') ? true : 'trail';

                    } else

                    if($current_date['day'] == $event_date['day']) {

                        $result = ($return == 'boolean') ? true : 'today';

                    } else

                    if($current_date['day'] > $event_date['day']) {

                        $result = false;

                    }

                } else

                if($current_date['month'] > $event_date['month']) {

                    $result = false;

                }

            } else

            if($current_date['year'] > $event_date['year']) {

                $result = false;

            }

 

Link to comment
Share on other sites

A simpler way to compare dates is as strings:

 

$current_date_str = "{$current_date['year']}-{$current_date['month']}-{$current_date['day']}";
$event_date_str = "{$event_date['year']}-{$event_date['month']}-{$event_date['day']}";
if ($current_date_str < $event_date_str) {
   # Too early
} elseif ($current_date_str == $event_date_str) {
  # Today
} elseif ($current_date_str > $event_date_str) {
  # After today, need to see if it's before the end date
}

 

That'll make your code much simpler.  As for events recurring at intervals, I would break it down into a few operations:

 

1.  Check events and find when the next occurrence at or after the current day is

2.  Check if that next occurrence is today

 

A library like http://www.php.net/manual/en/datetime.add.php would help a lot for intervals of "1 calendar month" or "1 year".  For weeks and fortnights you can just do arithmetic with days.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.