Support

Home Forums Event Espresso Premium How to Show Start Time and End Time for Events

How to Show Start Time and End Time for Events

Posted: December 15, 2017 at 9:53 am


txhomedecor

December 15, 2017 at 9:53 am

There seem to be many posts on showing start and end times but I can’t seem to get this code functioning. I’ve tried several different methods with no luck (some are still commented out below). It doesn’t seem like it should be a difficult task either. Can you have a look and make any recommendations?

FYI, for now this is a just a function and shortcode call just to see if I could get the data. If I can get it working, i’ll likely separate it into an plugin.

I’ve also looked at the following posts as well as github for ideas with no luck
How to show START DATE/ END DATE and START TIME/ END TIME
Edit the Events Table View Template Add-on to show Start time and end time

Any help greatly appreciated as i’ve spent way too much time on this just trying to get the start and end times 🙂

Here is the code:


function ee_events_shortcode( $atts ) {
    $events = EEM_Event::instance()->get_all(
    	array(
        	'order_by' => array( 'EVT_visible_on' => 'DESC' )
    	)
	);
    
     // content-header
    echo "BEGIN:VCALENDAR" . "
"; echo "VERSION:2.0" . "
"; echo "PRODID:-//" . the_title() . "//NONSGML Events //EN" . "
"; echo "X-WR-CALNAME:" . the_title() . _e(' - Events','themeforce') . "
"; echo "X-ORIGINAL-URL:" . the_permalink() . "
"; echo "X-WR-CALDESC:" , the_title() . _e(' - Events','themeforce') . "
"; echo "CALSCALE:GREGORIAN" . "
"; foreach( $events as $event ) { global $post; //echo $event->name()."  " . "
"; echo "BEGIN:VEVENT" . "
"; // get the next upcoming start date from all the available datetimes //$external_url = $post->EE_Event->external_url(); //echo "Event URL:" . $external_url . "
"; $datetime_limit = $show_all_datetimes ? NULL : 1; // Pull the datetimes for this event order by start_date/time $datetimes = EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $event->ID, $show_expired, false, $datetime_limit ); // Reset the datetimes pointer to the earliest datetime and use that one. $datetime = reset( $datetimes ); if ($datetime instanceof EE_Datetime) { echo 'ID:' . $event->ID . '
'; echo 'Title:' . $event->post_title . '
'; echo 'Start Date:' . $datetime->get_raw( 'DTT_EVT_start' ) . '
'; echo 'End Date:' . $datetime->get_raw( 'DTT_EVT_end' ) . '
'; // Loop over each datetime we have pulled from the database and output foreach ($datetimes as $datetime) { echo 'Datetime ID:' . $datetime->ID() . '
'; echo 'Date:' . date_i18n( $date_format . ' ' . $time_format, strtotime( $datetime->start_date_and_time('Y-m-d', 'H:i:s') ) ) . '
'; echo 'Date:' . date_i18n( $date_format, strtotime( $datetime->start_date('Y-m-d') ) ) . '
'; echo 'Start Date:' . date_i18n( $time_format, strtotime( $datetime->start_time('H:i:s') ) ) . '
'; echo 'End Date:' . date_i18n( $time_format, strtotime( $datetime->end_time('H:i:s') ) ) . '
'; //end foreach $datetimes }; }; //$geteventdate = $event->first_datetime(); //$eventdate = date('Y-m-d H:i:s', strtotime($geteventdate->start_date())); //echo "DTSTART:" . $eventdate . "
"; //echo "DTSTART:"; //do_shortcode( '[TIME_START]' ); //echo "
"; //do_shortcode( '[ESPRESSO_EVENTS_TABLE_TEMPLATE show_all_datetimes=TRUE]' ); //echo "
"; //echo "DTEND:" . "" . "
"; //echo "DTEND:" . $event->get('DTT_EVT_end'). "
"; echo "SUMMARY:" . $event->name() . "
"; echo "DESCRIPTION:" . $event->name() . "
"; //echo "DESCRIPTION:" . $event->description() . "
"; echo "END:VEVENT" . "
"; $startdate = ""; } echo "END:VCALENDAR" . "
"; } add_shortcode( 'ee_events_shortcode_output', 'ee_events_shortcode');

Here is the results page
Results


txhomedecor

December 15, 2017 at 9:59 am

Results link didn’t show correctly above. Here’s updated one:
Results


Josh

  • Support Staff

December 15, 2017 at 12:44 pm

I’m sorry it’s really not easy to read your code within a forum reply. Can you post the entire contents of the code you’re working with into a pastebin or gist, then post a link here?


txhomedecor

December 15, 2017 at 2:53 pm

Thanks for the response. I will post it later as gist is blocked where i currently am.


txhomedecor

December 15, 2017 at 7:24 pm

Josh, here’s a link to the code. Thanks.

Github


Tony

  • Support Staff

December 19, 2017 at 4:16 am

Hi there,

There’s a lot of code in that gist that isn’t needed and/or doesn’t really make sense, before you even get to outputting the date/times you’re throwing multiple undefined notices (which is also why you can’t get an event date to output).


Notice: Undefined variable: show_all_datetimes
Notice: Undefined property: EE_Event::$ID
Notice: Undefined variable: show_expired

That’s from line 28 and just above it – $datetimes = EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $event->ID, $show_expired, false, $datetime_limit );

Which looks like it’s from one of my examples of a custom template for the table view add-on that’s been altered.

show_all_datetimes allows you to pull multiple datetimes from an event if you pas it to the shortcode, you aren’t currently initializing it so if you don’t pass it to the shortcode you get a notice.

EE_Event::$ID is from $event->ID as it should be a method, not a property, so it would be $event->ID() (the original code used $post->ID because the WP Post object in use in that code has the property, the EE EVent object your using, does not).

show_expired is basically the same problem as show_all_datetimes.

For line 28, if you want all of the datetimes from an event use:

EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $event->ID() );

However, that also includes expired datetimes, if you don’t want that use:

EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $event->ID(), false );

Change any instance of $event->ID to $event->ID().

$event->post_title should be $event->name()

Right now for the first start date your using $datetime->get_raw( 'DTT_EVT_start' ) which is the unix timestamp from the DB

You can use $datetime->start_date() or $datetime->end_date() for those values in a readable format.

(If you use Kint Debugger and wrap an object (in this example $datetime) in d(); you’ll get a really nice easy to follow output that shows all of the available properties/method on that object).

Fixing the undefined notices and initializing the variables you need will fix most of the problems you having, for example:

https://gist.github.com/Pebblo/23c231f46ffb84813b5a8511eff7f8b1

To answer your question on how to output the start and end date/times for an event, you pull the datetimes for the event and then use the method available on datetime object. How you do that depends on which of the datetimes in the event you want (include expired? include archived? next upcoming? ‘First’ datetime? as you can see theres a lot of ways in which users want to pull datetimes)


txhomedecor

December 19, 2017 at 11:57 am

Thanks Tony, i’ll be having a look but have a better understanding. As you can see, I left all the “trial” code in there so you can see what i’ve attempted already – they weren’t all utilized at the same time.


Tony

  • Support Staff

December 20, 2017 at 4:54 am

Yeah, I could see the additional code, but it was the code being used to pull in the datetimes that caused the issue you were having, you had no datetimes (because the variables being passed (mainly the Event ID) were not set so the function didn’t know what event to pull from).

The above gist should help point you in the right direction but please do let me know if you need further help.

The support post ‘How to Show Start Time and End Time for Events’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso