WordPress.org

Forums

The Events Calendar
Bug: All Day events don't have correct end time. (4 posts)

  1. g4ry
    Member
    Posted 4 years ago #

    I'm not sure if this has been discussed before, but it looks like I found a bug that potentially affects if an Event will be properly listed in the "Events List Widget" and the "List" view. This bug also seems to have the potential to affect any implementation of an Recurring Events feature in future versions.

    When an all-day event is entered for the first time with *no revisions*, the event's "_EventEndDate" meta is not properly recorded. The event's date is recorded correctly, but the time is not.

    Instead of:
    "YYYY-MM-DD 23:59:00" (the event ends on the last minute of the day)

    it is recorded as:
    "YYYY-MM-DD 00:00:00" (the event ends on the first minute of the day)

    Any revisions to the post will correct this error, but if the post is not modified, the error will remain.

    Because the event is recorded as ending on the first minute of the day, any event posted once with no revisions will not appear on the widget or list view on the day of the event because after the first minute of the day, the event will be detected as being over. Also, if the upcoming recurring events feature simply duplicates a post multiple times, this bug could affect the copies too.

    Temporary Workaround: The simple workaround is when entering a new all-day event, click "Publish" then after the post is entered, click "Update" so it gets a second revision.

  2. g4ry
    Member
    Posted 4 years ago #

    Okay, I think I've figured out why this bug occurs only for brand-new, unrevised posts.

    Being as lazy as I am, for all-day events I just set the "Start Date / Time:" drop-down selection to the date of my event and let the program validate the "End Date / Time:" for me.

    This bug seems to occur because the function "addEventMeta( $postId )" sets the "EventEndDate" for all-day events first, then it validates that "start date < end date" second. So in other words, it sets the 23:59:00 time for all-day events first, but then this gets re-written back to 00:00:00 when it later validates that the event end date should be later than the start date.

    Current code, lines 856-861 in the-events-calendar-class.php:

    // sanity check that start date < end date
    $startTimestamp = strtotime( $_POST['EventStartDate'] );
    $endTimestamp 	= strtotime( $_POST['EventEndDate'] );
    if ( $startTimestamp > $endTimestamp ) {
    	$_POST['EventEndDate'] = $_POST['EventStartDate'];
    }

    Line 860 should be changed to this so it preserves the time at 23:59:00 or whatever it was before, and the validation only changes the YYYY-MM-DD portion of the event's end date:

    $_POST['EventEndDate'] = substr_replace( $_POST['EventStartDate'], substr( $_POST['EventEndDate'], -8 ), -8 );

    I think this should fix it. I've been looking directly at the MySQL "wp_postmeta" table to first detect the bug and see if the above fix works and all seems okay now.

  3. g4ry
    Member
    Posted 4 years ago #

    After thinking about it, there is still potential for problems with this fix. For example, if the event is not an all-day event and the original start and end times were this:

    2011-05-10 10:00:00 (start)
    2011-01-01 08:00:00 (end)

    the modified code will fix it to this, which is still invalid:

    2011-05-10 10:00:00 (start)
    2011-05-01 08:00:00 (end)

    The end time is still before the start time. So after fixing the YYYY-MM-DD portion, the revised time needs to be checked again to see the end time is after the start time. Repeating the original test after the modified test should be a good final fix for that.

    // sanity check that start date < end date
    $startTimestamp = strtotime( $_POST['EventStartDate'] );
    $endTimestamp 	= strtotime( $_POST['EventEndDate'] );
    if ( $startTimestamp > $endTimestamp ) {
    	$_POST['EventEndDate'] = substr_replace( $_POST['EventStartDate'], substr( $_POST['EventEndDate'], -8 ), -8 );
    }
    $endTimestamp 	= strtotime( $_POST['EventEndDate'] );
    if ( $startTimestamp > $endTimestamp ) {
    	$_POST['EventEndDate'] = $_POST['EventStartDate'];
    }

    I haven't tested this code yet, but will report back later on what I find.

  4. g4ry
    Member
    Posted 4 years ago #

    Okay, I've got new code I'm happy with:

    // sanity check that start date < end date
    $startTimestamp = strtotime( $_POST['EventStartDate'] );
    $endTimestamp 	= strtotime( $_POST['EventEndDate'] );
    if ( $startTimestamp > $endTimestamp ) {
    	$_POST['EventEndDate'] = substr_replace( $_POST['EventStartDate'], substr( $_POST['EventEndDate'], -8 ), -8 );
    	$endTimestamp 	= strtotime( $_POST['EventEndDate'] );
    	if ( $startTimestamp > $endTimestamp ) {
    		$_POST['EventEndDate'] = $_POST['EventStartDate'];
    	}
    }

    This is slightly cleaner than the previous because properly set start and end times only get tested once, rather than twice. Incorrectly set start and end times get first tested if dates only need to be fixed, then it gets tested again to see if the time-of-day needs to be fixed too.

    I tested it with these pairs of start and end date/times:

    All day event:
    2011-08-08 18:00:00 (start)
    2011-05-08 18:00:00 (end)

    2011-08-08 18:00:00 (start)
    2011-05-08 13:00:00 (end)

    2011-08-08 18:00:00 (start)
    2011-05-08 21:00:00 (end)

    All entries resulted in valid entries where "start time < end time" on the first try without any revisions, and all-day events properly started at 00:00:00 and ended at 23:59:00.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • The Events Calendar
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic