scrubbicus Posted March 3, 2011 Share Posted March 3, 2011 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 https://forums.phpfreaks.com/topic/229441-creating-a-calendar-with-reoccuring-events/ Share on other sites More sharing options...
btherl Posted March 3, 2011 Share Posted March 3, 2011 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 https://forums.phpfreaks.com/topic/229441-creating-a-calendar-with-reoccuring-events/#findComment-1182168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.