Date comparisons
-
I know this is a “me” problem, not an EventPrime issue, but I’m hoping someone here is willing to help me. I am (obviously) not a php programmer.
Given this code:
$now = date_create();
$eventend = get_post_meta( $event_id, ’em_end_date_time’, true );
echo ‘today = ‘;
echo $now->format(‘m/d/Y’) . ”;
echo ‘ – end = ‘ . date(‘m/d/Y’, $eventend) . ”;outputs this:
today = 12/29/2025 – end = 01/12/2026but the test “if ($eventend < $now)” is “true” (no matter what the value of $eventend is). Given the data above, this should definitely be “false”. I’m _guessing_ $now is not in the same format as $eventend. How do I convert $now to whatever format $eventend is? (Or vice-versa?)
Thanks in advance for any ideas anyone is willing to share.
-
Thanks for reaching out to us.
This is happening because you’re comparing two different data types, not because of EventPrime
$now = date_create(); // DateTime object
$eventend = get_post_meta(…); // Unix timestamp (integer)
So when you do:
if ( $eventend < $now )
PHP is comparing an integer to a DateTime object, which leads to unreliable results (and in this case always evaluates as true).
Your output looks correct because you’re formatting each value only for display, but internally they’re not comparable.You need to Convert $now to a Unix timestamp so both values match:
$now = time();
$eventend = (int) get_post_meta( $event_id, ’em_end_date_time’, true );
if ( $eventend < $now )
{
// event has ended
}If you have any further questions, please don’t hesitate to reach out to us.
I know it was not an EventPrime issue (and said so in my original question). But thank you for responding anyway. Your support is always great.
What I was attempting to do is modify the “simple list” previously submitted (not by me) and modify it to list events for a limited number of day into the future. For example, I wanted a list of all events that will be happening in the next 2 weeks (14 days). That list should include events that are already started or will start within the selected number of days.
Here’s the code. I hope others find it useful.
/**
- Shortcode: [ep_simple_list_days]
- Displays a simplified list of EventPrime events for a number of days
* - Attributes:
- – type=”” → event type slug (EventPrime taxonomy)
- – limit=14 → number of days to display (must be at least 1)(default = 14)
- – order=”ASC” → sorting order “ASC” or “DESC” (default=”ASC”)
- – date=”true” → display event date (default=true)
- – venue=”true” → display event venue (default=true)
- – organizer=”true” → display organizer (default=true)
*/
function simple_eventprime_list_days( $atts ) {
$atts = shortcode_atts( array( 'type' => 'Run', 'limit' => 14, 'order' => 'ASC', 'date' => 'true', 'venue' => 'true', 'organizer' => 'true', ), $atts ); $args = array( 'post_type' => 'em_event', 'posts_per_page' => -1, 'meta_key' => 'em_start_date_time', 'orderby' => 'meta_value_num', 'order' => $atts['order'], ); if ( ! empty( $atts['type'] ) ) { $args['tax_query'] = array( array( 'taxonomy' => 'em_event_type', 'field' => 'slug', 'terms' => $atts['type'], ), ); } $query = new WP_Query( $args ); $upcoming_events = array(); $past_events = array(); $now = time(); $enddate = time() ; $daylimit = $atts['limit']; $now2 = date_i18n( 'm/d/Y', $now ) ; $enddate2 = date_i18n( 'm/d/Y', $enddate ) ; $enddate3 = ($enddate + ($daylimit*24*60*60)) ; $enddate4 = date_i18n('m/d/Y', $enddate3 ) ; $listing_page = get_page_by_path( 'all-events' ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $event_id = get_the_ID(); $eventstart = (int) get_post_meta( $event_id, 'em_start_date_time', true ); $eventend = (int) get_post_meta( $event_id, 'em_end_date_time', true );/* test for events that have ended before today */
if ( $eventend < $now ) {
continue;
}/* test for events that start after the end date of the reporting period */
if ( $eventstart > $enddate3 ) {
continue ;
}$upcoming_events[] = $event_id; } } wp_reset_postdata(); ob_start(); /** * Event list renderer */ $display_events = function ( $event_ids, $title ) use ( $atts, $listing_page ) { if ( empty( $event_ids ) ) { echo '*** No Events ***' ; return; } echo '<h5 style="font-weight:bold;">' . esc_html( $title ) . '</h5>'; echo '<ul class="simple-eventprime-list">'; $count = 0; foreach ( $event_ids as $event_id ) { $event_title = get_the_title( $event_id ); $event_link = $listing_page ? add_query_arg( 'event', $event_id, get_permalink( $listing_page->ID ) ) : get_permalink( $event_id ); echo '<li>'; echo '<strong><a class="simple-eventprime-link" href="' . esc_url($event_link) .'" ><em>' . esc_html( $event_title ) . '</em></a></strong>'; /* Date */ if ( $atts['date'] === 'true' ) { $start = get_post_meta( $event_id, 'em_start_date_time', true ); $end = get_post_meta( $event_id, 'em_end_date_time', true ); if ( $start ) { echo ': ' . date( 'm/d/Y', $start ); } if ( $end ) { echo ' - ' . date( 'm/d/Y', $end ); } } /* Organizer (using EventPrime core function) */ if ( $atts['organizer'] === 'true' && class_exists( 'EventPrime_Basic_Functions' ) ) { $basic = new EventPrime_Basic_Functions(); $organizer_ids = get_post_meta( $event_id, 'em_organizer', true ); $organizers = ! empty( $organizer_ids ) ? $basic->ep_get_event_organizer( $organizer_ids ) : array(); if ( ! empty( $organizers ) && is_array( $organizers ) ) { $links = array(); foreach ( $organizers as $org ) { if ( ! empty( $org->name ) ) { if ( ! empty( $org->url ) ) { $links[] = 'url ) . >' . esc_html( $org->name ) . ''; } else { $links[] = esc_html( $org->name ); } } } if ( ! empty( $links ) ) { echo ' presented by <b>' . implode( ', ', $links ) . '</b>'; } } } /* Venue */ if ( $atts['venue'] === 'true' ) { $venue_terms = get_the_terms( $event_id, 'em_venue' ); if ( $venue_terms && ! is_wp_error( $venue_terms ) ) { echo ' at ' . esc_html( implode( ', ', wp_list_pluck( $venue_terms, 'name' ) ) ); } } echo '</li>'; } echo '</ul>'; }; $display_events( $upcoming_events, 'Upcoming Performances' ); if ( empty( $upcoming_events )) { /* && empty( $past_events ) ) { */ echo '<p class="simple-eventprime-none">No events found.</p>'; } return ob_get_clean();}
add_shortcode( ‘ep_simple_list_days’, ‘simple_eventprime_list_days’ );
Thanks for the update.
If you have any further questions, please don’t hesitate to reach out to us.
Copying and pasting the code above doesn’t work very well. I have created two variations of the “simple list” shortcode. One can limit by nunber of events, the other by number of days. They are available here: https://www.dropbox.com/scl/fi/2x9ao6xejd3ecb9igowis/ep_simple_list_2.txt?rlkey=lirfu10jkyru94bcmgcrp3dj7&dl=0
If you enhance any of these, I’d appreciate you sharing the results with me.
Larry
You must be logged in to reply to this topic.