Support

Home Forums Event Espresso Premium Grid view: last datetime

Grid view: last datetime

Posted: January 19, 2021 at 9:01 am

Viewing 4 reply threads


ComedieTriomphe

January 19, 2021 at 9:01 am

Hi,

I am forking the latest grid-view-template addon to fit my client’s needs.

For any event on the grid, I want to display the earliest (first) datetime, as well as the last datetime. For instance:

Event X datetimes:
– 19/01
– 23/01
– 30/01

Expected output:

19 January 2021 – 30 January 2021

Now, looking at the espresso-grid-template.template.php file, I can see the datetime variable is initialized with the datetimes array (line 37). My first thought was that I could find the last datetime in this array, but having had a look at the array, it seems that’s not the right approach.

Here is the original EE code which I believe can be tweaked to access the last datetime of any given event:

$datetimes = EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $post->ID, $show_expired, false, 1 );

$datetime = end( $datetimes );
if ($datetime instanceof EE_Datetime) {

$startdate = date_i18n( $date_format . ' ' . $time_format, strtotime( $datetime->start_date_and_time('Y-m-d', 'H:i:s') ) );
?>
}

Could you help me figure out how to access the last datetime of a given event?

Thanks!


ComedieTriomphe

January 19, 2021 at 9:04 am

Can’t edit, but here is a much better formatting of the code on pastebin:

https://pastebin.com/81wqhrCm


Tony

  • Support Staff

January 20, 2021 at 6:09 am

Hi there,

If you take a look at the get_datetimes_for_event_ordered_by_start_time method you’ll see the last parameter is a limit value, which is 1 above (meaning it will only pull the lastest datetime):

get_datetimes_for_event_ordered_by_start_time(
    $EVT_ID,
    $include_expired = true,
    $include_deleted = true,
    $limit = null
)

So if you remove the 1 from that function call you’ll get an array with all datetimes, then you’ll just need the first and last element from the array.

Another option is to use the helper methods we have for pulling in the ‘earliest’ and ‘latest’ datetimes from the event:

$earliest_dtt = EEH_Event_View::get_earliest_date_obj($EVT_ID);
$latest_dtt = EEH_Event_View::get_latest_date_obj($EVT_ID);

(Note that both are ordered by DTT_EVT_Start, meaning the start date set on the datetime)


ComedieTriomphe

January 20, 2021 at 7:36 am

This makes sense! I was wondering if it had to do with that argument.

Thank you I will try that and hopefully get it to work!

Is there any part of the documentation which details the functions and their arguments? Or maybe a file in the core which has all the core definitions in there? Just for future reference.

I had a look in the developer portal but no luck!


Tony

  • Support Staff

January 20, 2021 at 7:54 am

Not currently no, we don’t have a full list of all of the method available within Event Espresso at this time.

Have you read through the docs on the model system?

https://github.com/eventespresso/event-espresso-core/tree/master/docs/G–Model-System

Learning how that model system works means you don’t need to rely on specific functions etc. For example, the above helper functions I gave you are just using the model system to pull the information in for you.

A quick example of get_earliest_date_obj():

public static function get_earliest_date_obj($EVT_ID = 0)
{
    $event = EEH_Event_View::get_event($EVT_ID);
    if ($event instanceof EE_Event) {
        $datetimes = $event->get_many_related(
            'Datetime',
            array(
                'limit' => 1,
                'order_by' => array( 'DTT_EVT_start' => 'ASC' )
            )
        );
        return reset($datetimes);
    } else {
         return false;
    }
}

It’s pulling in the EE_Event model object, then using the get_many_related() method to pull in related Datetimes based on the query args passed in.

The only difference between that function and get_latest_date_obj() is it orders DESC:

'order_by' => array( 'DTT_EVT_start' => 'DESC' )

So learning how to use those models will be much more beneficial to you than specific functions as we won’t have specific functions for everything you want to do, but 99% of it you’ll already be able to do using the models.

Viewing 4 reply threads

The support post ‘Grid view: last datetime’ 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