MyBB Community Forums

Full Version: [Calendar] Event not shown in month view [R]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Of course I could not try all the possible combinations of variables. I have tested out only those involved in the changes I made. However I feel so sure now because the code is simpler than before. For example take a look at this snippet of code:

Old code:
	// Start constructing the calendar

	$weekdays = fetch_weekday_structure($calendar['startofweek']);

	$month_start_weekday = gmdate("w", gmmktime(0, 0, 0, $month, $calendar['startofweek']+1, $year));
	
	// This is if we have days in the previous month to show
	if($month_start_weekday != $weekdays[0] || $calendar['startofweek'] != 0)
	{
		$day = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
		$day -= array_search(($month_start_weekday), $weekdays);
		$day += $calendar['startofweek']+1;
		$calendar_month = $prev_month['month'];
		$calendar_year = $prev_month['year'];
	}
	else
	{
		$day = $calendar['startofweek']+1;
		$calendar_month = $month;
		$calendar_year = $year;
	}

My code:
	// Start constructing the calendar

	$weekdays = fetch_weekday_structure($calendar['startofweek']);

	$month_start_weekday = gmdate("w", gmmktime(0, 0, 0, $month, 1, $year));	

	// This is if we have days in the previous month to show
	if($month_start_weekday != $weekdays[0])
	{
		$day = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
		$day -= array_search($month_start_weekday, $weekdays) - 1;
		$calendar_month = $prev_month['month'];
		$calendar_year = $prev_month['year'];
	}
	else
	{
		$day = 1;
		$calendar_month = $month;
		$calendar_year = $year;
	}

In the old code, under certain circumstances, the variable $day reached 32 or 33 causing unpredictable behaviors. This is the main cause of the bug reported in the first post.
(2009-06-06, 10:59 AM)Gionet82 Wrote: [ -> ]Of course I could not try all the possible combinations of variables. I have tested out only those involved in the changes I made. However I feel so sure now because the code is simpler than before. For example take a look at this snippet of code:

Old code:
	// Start constructing the calendar

	$weekdays = fetch_weekday_structure($calendar['startofweek']);

	$month_start_weekday = gmdate("w", gmmktime(0, 0, 0, $month, $calendar['startofweek']+1, $year));
	
	// This is if we have days in the previous month to show
	if($month_start_weekday != $weekdays[0] || $calendar['startofweek'] != 0)
	{
		$day = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
		$day -= array_search(($month_start_weekday), $weekdays);
		$day += $calendar['startofweek']+1;
		$calendar_month = $prev_month['month'];
		$calendar_year = $prev_month['year'];
	}
	else
	{
		$day = $calendar['startofweek']+1;
		$calendar_month = $month;
		$calendar_year = $year;
	}

My code:
	// Start constructing the calendar

	$weekdays = fetch_weekday_structure($calendar['startofweek']);

	$month_start_weekday = gmdate("w", gmmktime(0, 0, 0, $month, 1, $year));	

	// This is if we have days in the previous month to show
	if($month_start_weekday != $weekdays[0])
	{
		$day = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
		$day -= array_search($month_start_weekday, $weekdays) - 1;
		$calendar_month = $prev_month['month'];
		$calendar_year = $prev_month['year'];
	}
	else
	{
		$day = 1;
		$calendar_month = $month;
		$calendar_year = $year;
	}

In the old code, under certain circumstances, the variable $day reached 32 or 33 causing unpredictable behaviors. This is the main cause of the bug reported in the first post.

Yeah, and you know what you also did? You broke the "Start Of Day" functionality.

That fix is invalid.
(2009-06-06, 09:09 PM)Ryan Gordon Wrote: [ -> ][...]
Yeah, and you know what you also did? You broke the "Start Of Day" functionality.

That fix is invalid.

Don't worry "Start Of Day" feature is safe and works pretty good with that fix.
Trust me, these fixes work very well. I'm not the only one who says that:

(2009-06-05, 02:34 PM)Tomm M Wrote: [ -> ][...]
I've tested out the various fixes and they seem pretty good to me. They all fix the issues mentioned and you've even hit ones not noticed before.
[...]

If there is something that you don't understand in the files that I have posted ask and I'll reply if I can. On the contrary if there is some reason why my contribution is not appreciated tell me and I'll not spend another second of my time here. Dodgy
(2009-06-07, 07:43 AM)Gionet82 Wrote: [ -> ]
(2009-06-06, 09:09 PM)Ryan Gordon Wrote: [ -> ][...]
Yeah, and you know what you also did? You broke the "Start Of Day" functionality.

That fix is invalid.

Don't worry "Start Of Day" feature is safe and works pretty good with that fix.
Trust me, these fixes work very well. I'm not the only one who says that:

You still do need to take into account the "Start Of Day" within the code.
(2009-06-07, 07:53 AM)Ryan Gordon Wrote: [ -> ]
(2009-06-07, 07:43 AM)Gionet82 Wrote: [ -> ]
(2009-06-06, 09:09 PM)Ryan Gordon Wrote: [ -> ][...]
Yeah, and you know what you also did? You broke the "Start Of Day" functionality.

That fix is invalid.

Don't worry "Start Of Day" feature is safe and works pretty good with that fix.
Trust me, these fixes work very well. I'm not the only one who says that:

You still do need to take into account the "Start Of Day" within the code.

First of all, are we speaking of the same thing? With the term "Start Of Day" you mean "Start of Week", is it right? In this case I'm going to explain how my code takes into account also the "Start of week" functionality. Please read this carefully before reply me.

For convenience I quote the snippet of code here:
   // Start constructing the calendar

    $weekdays = fetch_weekday_structure($calendar['startofweek']);

    $month_start_weekday = gmdate("w", gmmktime(0, 0, 0, $month, 1, $year));    

    // This is if we have days in the previous month to show
    if($month_start_weekday != $weekdays[0])
    {
        $day = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
        $day -= array_search($month_start_weekday, $weekdays) - 1;
        $calendar_month = $prev_month['month'];
        $calendar_year = $prev_month['year'];
    }
    else
    {
        $day = 1;
        $calendar_month = $month;
        $calendar_year = $year;
    } 


The variable $calendar['startofweek'] contains the start day of the week and it comes from the database. It is used to build the array $weekdays trough the function fetch_weekday_structure(). So $weekdays contains an ordered list of weekdays depending on a specified starting day.
Here there is the definition of this function: http://crossreference.mybboard.de/nav.ht....html#l546

Please notice that $calendar['startofweek'] and $weekdays[0] always contain the same value. So when we check if($month_start_weekday != $weekdays[0]) we are comparing the day of the week in which the current month starts and our start day of the week. Indeed you could equivalently write it as:
if($month_start_weekday != $calendar['startofweek'])
    {... 
Only this (whitout || $calendar['startofweek'] != 0) is enough to understand if there are days of the previous month to show at the beginning of the calendar table.

So if the day of the week in which the month starts(e.g. Monday) and our start day of the week (e.g. ...Monday) match, there are no days of the previous month to show and the starting point is:
		$day = 1;
		$calendar_month = $month;
		$calendar_year = $year;
Otherwise if they don't match, the starting point is in the previous month and we have to calculate an offset of days from the end of the month.
		$day = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
		$day -= array_search($month_start_weekday, $weekdays) - 1;
		$calendar_month = $prev_month['month'];
		$calendar_year = $prev_month['year'];
The offset is:

array_search($month_start_weekday, $weekdays) - 1

that is the position(the index) of $month_start_weekday in our ordered list of weekdays (minus one). With a real calendar in front of you it should be very easy to understand. Notice that is true for any start day of the week, even Sunday or Saturday.
Other additions like $day+=$calendar['startofweek']+1 in the old code may cause an overflow of days (32,33,34...). This is the main cause of the reported bug, as the fetching of the events follows that snippet of code.

I hope that you believe me now. Smile
Pages: 1 2