Trouble understanding start_date in tribe_get_events()

Home Forums Calendar Products Events Calendar PRO Trouble understanding start_date in tribe_get_events()

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #1543675
    sabinevi
    Participant

    Hi,

    Im trying to show upcoming events with:

    tribe_get_events( array( 'start_date' => date('Y-m-d H:i:s'), 'posts_per_page' => -1 ) )

    but I get also events that have their start_date in the past but with an end_date in the future. Is there a way to to filter those out in the arguments of this function-call?

    #1544899
    Cliff
    Member

    Hi. Thanks for sharing your code; that really helps.

    What may actually be happening is that the PHP date() function is based on UTC, while your site’s time zone appears to be Europe/Amsterdam. You might try WP’s current_time() instead, or you could change

    date('Y-m-d H:i:s')

    to this:

    $now_amsterdam = new DateTime( 'now', new DateTimeZone( 'Europe/Amsterdam' ) );
    $events_not_yet_started = tribe_get_events( array( 'start_date' => $now_amsterdam->format( 'Y-m-d H:i:s' ), 'posts_per_page' => -1 ) );
    

    See http://php.net/manual/en/class.datetime.php and http://php.net/manual/en/class.datetimezone.php for reference.

    And, of course, I recommend referring to our own https://theeventscalendar.com/knowledgebase/using-tribe_get_events/ if you haven’t already.

    Finally, we always recommend restorable database and file backups as well as performing modifications first on a staging site before modifying anything on your live/production site.

    Please let me know how this goes for you.

    #1545056
    sabinevi
    Participant

    Hi Cliff,

    Thanks for your reply! I don’t think the timezone is the issue here. This website deals with art-exhibitions that most of the time end several months after they start so I think it’s a date-problem, not a time-problem.
    If you have a look at my testpage (where I use this tribe_get_events) you can see that the events there have start dates between January 2018 and July 2018. I would expect to see only the top six events since those are the only ones that start after today (June 3rd). What am I missing here?

    #1545288
    sabinevi
    Participant

    I have to add that I have an alternative ordering set in the functions.php:

    function tribe_post_alternatieve_sortering( $query ) {
    	
        	$query->set( 'orderby', 'start_date' );
    	$query->set( 'order', 'DESC' );
    
    	return $query;
    }
    
    add_action( 'pre_get_posts', 'tribe_post_alternatieve_sortering', 51 );
    #1546889
    Cliff
    Member

    Hi. Sorry you haven’t figured this out yet.

    Firstly, you should use the tribe_events_pre_get_posts hook for that last snippet.

    Secondly, I’m unsure of the full code used to produce your test site, but it outputs an extra colon: https://cl.ly/0F0U2O1H1u14

    Could you please provide me the entire snippet used to generate that test page’s content?

    #1547148
    sabinevi
    Participant

    Thanks for the correction of the ordering snippet!

    This is the complete code of the testpage template:

    
    <?php
    /*
    Template Name: testevents
    */
    
    get_header();
    
    $now_amsterdam = new DateTime( 'now', new DateTimeZone( 'Europe/Amsterdam' ) );
    $aEvents = tribe_get_events( array( 'start_date' => $now_amsterdam, 'posts_per_page' => -1 ) );
    
    echo '<table border="1"><tr><td>title</td><td>start</td><td>end</td></tr>';
    
    foreach( $aEvents as $o ) {
    	echo '<tr><td><b>'.$o->post_title.' </b></td>';
    	echo '<td>'.$o->EventStartDate.' </td>';
    	echo '<td>: '.$o->EventEndDate.'</td></tr>';
    	
    }
    echo '</table>';
    
    get_footer();
    
    #1547971
    Cliff
    Member

    You need to change ‘start_date’ to be:

    $now_amsterdam->format( 'Y-m-d H:i:s' )
    #1550563
    Christophe Rocour
    Participant

    Hi Cliff, this is sabinevi from another account since the license of EC of my own account was not continued by the customer whom I bought it for.

    I changed the date to

    
    $now_amsterdam->format( 'Y-m-d H:i:s' )
    

    but that didn’t solve it for me. Any other thoughts on this subject?

    #1551005
    Cliff
    Member

    You’ll need to figure out your custom code on your own, per our Scope of Support / Terms.

    I’d suggest outputting all the various parts using var_dump()

    Feel free to share any error messages or unexpected results from var_dump()

    #1552123
    Christophe Rocour
    Participant

    I know that I have to figure out my own custom coding but through this post I am trying to understand how the events calendar ‘calculates’ the start date in events that span multiple days (like the events on our site).

    I’m using the example found in your knowledge base but I’m also getting events that have already started in the past instead of only the ones that will start today and in the future as I would expect. I don’t understand that (see my initial post). If you could just point to the part of the documentation where the start_date is explained, it would be very helpful and I will work the custom coding out myself.

    #1552821
    Cliff
    Member

    I was incorrect in my previous reply. You can actually send a full DateTime() object to start_time: https://github.com/moderntribe/the-events-calendar/blob/4.6.18/src/Tribe/Query.php#L283-L289

    You could do this:

    $now_amsterdam = new DateTime( 'now Europe/Amsterdam' );
    $aEvents = tribe_get_events( array( 'start_date' => $now_amsterdam, ...as you had before...

    Please let me know how this goes for you.

     

    #1553833
    Christophe Rocour
    Participant

    I’m so sorry that my English is too bad to explain clearly what my question is. I will try one final time.

    Our website is http://foto-agenda.nl . It’s a website where photo exhibitions are listed. These exhibitions are the events. They start on a different date than they end. Start time and end time are not relevant and not in use.

    On my testpage I’m trying to show a list of all events that will have their start date set to today or in the future. So NOT events that started on month ago and are still going on because they end in the future, only the events that have their first date (what I call their start date) today or later.
    I am using a slightly altered version of this example I found in your knowledge base:

    
    // Retrieve the next 5 upcoming events
    $events = tribe_get_events( array(
     'posts_per_page' => 5,
     'start_date' => date( 'Y-m-d H:i:s' )
     ) );
    

    My version:

    
    // Retrieve all events that have set their start date today or later
    $now_amsterdam = new DateTime( 'now', new DateTimeZone( 'Europe/Amsterdam' ) );
    $aEvents = tribe_get_events( array( 
    'start_date' => $now_amsterdam, 
    'posts_per_page' => -1 
    ) );
    

    But as you can see on the testpage there are events listed that have their start date in the past and THAT is what I don’t understand. Why does this query give me events that start in the past while I expect that I only would get events that start today or later?

    Could it be that an event that started in the past and has its end date in the future, is considered as having a start date today? Is that the logic behind the (for me) unexpected results? And if so, how do I filter out events that have their first start date set in the past?

    I hope I explained it more clearly now, thank you for your patience!

    #1553923
    Cliff
    Member

    Sorry this has gone on so long. I wonder if this is closer to what you want — remove the ‘featured’ => array(…) part:

    https://gist.github.com/cliffordp/1b8c808efa4256ebcef2d8805f069595

    P.S. I noticed your license is expired. Were you planning to renew? Did you have any troubles with that?

    #1554069
    Christophe Rocour
    Participant

    THANK YOU Cliff, this works like a charm! Now I get the results I expect!

    I will explain my license issue in a private reply hereafter.

    #1554070
    Christophe Rocour
    Participant

    This reply is private.

Viewing 15 posts - 1 through 15 (of 16 total)
  • The topic ‘Trouble understanding start_date in tribe_get_events()’ is closed to new replies.