Support

Home Forums Event Espresso Premium Show Events within certain date range

Show Events within certain date range

Posted: November 7, 2017 at 7:57 am

Viewing 7 reply threads


jacl

November 7, 2017 at 7:57 am

Hi
Using EE4 and need to show a table of events for admins only and our default date range we want to show are any events in the past 5 days and also any events starting in in the next 16 hours.

Old system used this SQL

WHERE SQL : archive = 0 AND ( now() > DATE_SUB( dtstart, INTERVAL 16 HOUR ) AND now() < DATE_ADD( dtstart, INTERVAL 5 DAY ) )

Does anyone have an example of displaying a date range like this in a table…. and ideally would be great for admins to have an override at the top such as a drop down that said show: last 7 days, last 14 days, last month.

We are using Chronoforms plugin to build our own frontend admin area to display these adminpages so its not in the WP dashboard.

thanks
Jonathan


Josh

  • Support Staff

November 7, 2017 at 2:51 pm

Hi there,

The EE4 developer documentation for Model querying shows some examples and explains how to use the model system to query Datetime fields instead of writing direct db queries:

https://github.com/eventespresso/event-espresso-core/blob/master/docs/G–Model-System/model-querying.md#querying-datetime-fields


jacl

November 7, 2017 at 3:17 pm

Thanks Josh

Ill take a look in the morning and let you know how I get on πŸ™‚


jacl

November 8, 2017 at 4:34 pm

Hi Josh

Well I think I spent several hours today reading those great docs on EE Models and Date times etc.

I have a simple query now working and displaying events but the timezone is all wrong. Its fine on the main site on the upcoming events widgets, shortcodes but not in my test code.

I am in UK so GMT timezone. My server is in Chicago and I have WordPress set to America/Chicago timezone.

So I just created an event to start at 08:00 am. In the SQL table it is stored as 2017-11-25 14:00:00. Chicago is 6 hours behind GMT so I guess as per the docs worpdress stores the date as GMT time.

In my code i’m doing this to simply show events between two dates….

EEM_Event::instance()->show_next_x_db_queries(1); //output SQL

$events = EEM_Event::instance()->get_all(
                array(
                    array(
                        'Datetime.DTT_EVT_start*1' => array('<', new \EventEspresso\core\domain\entities\DbSafeDateTime('2017-12-19 00:01', new DateTimeZone('America/Chicago'))),
                        'Datetime.DTT_EVT_start*2' => array('>', new \EventEspresso\core\domain\entities\DbSafeDateTime('5 days ago', new DateTimeZone('America/Chicago')))
                    )
                )
            );
echo "<br><br><br>";
foreach( $events as $event ) {
	
	$datetime = $event->get_first_related( 'Datetime' );
	
	echo $event->name()."<br>";
	echo $datetime->get_i18n_datetime( 'DTT_EVT_start', 'M d, Y H:i'  )." (".$event->get_timezone().")<br><br>";
}

It’s working but the SQL date times don;t match what I’m asking, notice here the SQL outputs a time of 06:01:00 when I used 00:01 in my code. so again this is out by 6 hours the wrong way so looks like its showing that time in GMT?

AND Datetime.DTT_EVT_start < '2017-12-19 06:01:00' AND Datetime.DTT_EVT_start > '2017-11-03 22:20:48'

and when the events are displayed they show timezone in use is correct America/Chicago but the time of the event instead of 08:00am shows as 02:00am? I could understand if it showed 14:00 as per the SQL table GMT time but this is showing 6 hours the wrong way.

BRC Clarksville
Nov 18, 2017 02:00 (America/Chicago)

Please help me to realise what I;m doing wrong in terms of timezones here πŸ™‚

Thank you so much

Jonathan


Josh

  • Support Staff

November 8, 2017 at 4:50 pm

Hi Jonathan,

Since Event Espresso support staff are not developers, it’d be best to open an issue on Github, then put a Question label on the issue. One of the developers can follow up with your coding-related question.


jacl

November 9, 2017 at 2:45 am

Hi Josh

Ahh I’ve seen all the great code snippets you’ve put out there…..so am pretty sure you know your stuff πŸ™‚

I’m not trying anything complicated here just to display some events between two start dates using the code supplied in your plugin docs.

My query is do you have any idea why when I submit a query with this date time

DbSafeDateTime(‘2017-12-19 00:01

the actual SQL generated by EE is showing a different time of 6 hours difference?

Datetime.DTT_EVT_start < ‘2017-12-19 06:01:00’

it must be a simple timezone issue so am i missing something form the few lines of code above to get this in to the correct timezone?

Thanks…. if you can’t hep on this simple query I’ll have to post on github.

Jonathan


jacl

November 9, 2017 at 9:23 am

Hi Josh….. all sorted!

It was the chronoforms plugin that was affecting the times. I found this line of code and have now commented it out in the plugin and all working as expected now.

date_default_timezone_set(get_option('timezone_string'));

Took me almost two days to work that one out – sigh! πŸ˜‰

thanks


Josh

  • Support Staff

November 9, 2017 at 3:09 pm

You could share this information with the plugin author of the chronoforms plugin, and maybe they’ll include a better way to get the current time in their plugin:

https://weston.ruter.net/2013/04/02/do-not-change-the-default-timezone-from-utc-in-wordpress/

https://vip.wordpress.com/documentation/vip-development-tips-tricks/use-current_time-not-date_default_timezone_set/

Viewing 7 reply threads

The support post ‘Show Events within certain date range’ 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