Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change date format for The Event Calendar single event view (M, D - D, Y
add_filter('tribe_events_event_schedule_details', 'custom_tribe_events_event_schedule_details');
function custom_tribe_events_event_schedule_details() {
if ( is_null( $event ) ) {
global $post;
$event = $post;
}
if ( is_numeric( $event ) )
$event = get_post( $event );
$schedule = '<span class="date-start dtstart">';
$format = '';
$date_without_year_format = tribe_get_date_format();
$date_with_year_format = tribe_get_date_format( true );
$time_format = get_option( 'time_format' );
$datetime_separator = tribe_get_option('dateTimeSeparator', ' @ ');
$time_range_separator = tribe_get_option('timeRangeSeparator', ' - ');
$microformatStartFormat = tribe_get_start_date( $event, false, 'Y-m-dTh:i' );
$microformatEndFormat = tribe_get_end_date( $event, false, 'Y-m-dTh:i' );
$settings = array(
'show_end_time' => true,
'time' => true,
);
$settings = wp_parse_args( apply_filters('tribe_events_event_schedule_details_formatting', $settings), $settings );
if ( ! $settings['time'] ) $settings['show_end_time'] = false;
extract($settings);
$format = $date_with_year_format;
// if it starts and ends in the current year then there is no need to display the year
if ( tribe_get_start_date( $event, false, 'Y' ) === date( 'Y' ) && tribe_get_end_date( $event, false, 'Y' ) === date( 'Y' ) ) {
$format = $date_without_year_format;
}
if ( tribe_event_is_multiday( $event ) ) { // multi-date event
$format2ndday = $format;
//If it's all day and the end date is in the same month and year, just show the day.
if ( tribe_event_is_all_day( $event ) && tribe_get_end_date( $event, false, 'm' ) === tribe_get_start_date( $event, false, 'm' ) && tribe_get_end_date( $event, false, 'Y' ) === date( 'Y' ) ) {
$format2ndday = 'j';
}
if ( tribe_event_is_all_day( $event ) ) {
$schedule .= tribe_get_start_date( $event, true, $format );
$schedule .= '<span class="value-title" title="'. $microformatStartFormat .'"></span>';
$schedule .= '</span>'.$time_range_separator;
$schedule .= '<span class="date-end dtend">';
$schedule .= tribe_get_end_date( $event, true, $format2ndday );
$schedule .= '<span class="value-title" title="'. $microformatEndFormat .'"></span>';
} else {
$schedule .= tribe_get_start_date( $event, false, $date_without_year_format );
$schedule .= '<span class="value-title" title="'. $microformatStartFormat .'"></span>';
$schedule .= '</span>'.$time_range_separator;
$schedule .= '<span class="date-end dtend">';
$schedule .= tribe_get_end_date( $event, true, 'j, Y' );
$schedule .= '<span class="value-title" title="'. $microformatEndFormat .'"></span>';
}
} elseif ( tribe_event_is_all_day( $event ) ) { // all day event
$schedule .= tribe_get_start_date( $event, true, $format );
$schedule .= '<span class="value-title" title="'. $microformatStartFormat .'"></span>';
} else { // single day event
if ( tribe_get_start_date( $event, false, 'g:i A' ) === tribe_get_end_date( $event, false, 'g:i A' ) ) { // Same start/end time
$schedule .= tribe_get_start_date( $event, false, $format ) . ( $time ? $datetime_separator . tribe_get_start_date( $event, false, $time_format ) : '' );
$schedule .= '<span class="value-title" title="'. $microformatStartFormat .'"></span>';
} else { // defined start/end time
$schedule .= tribe_get_start_date( $event, false, $format ) . ( $time ? $datetime_separator . tribe_get_start_date( $event, false, $time_format ) : '' );
$schedule .= '<span class="value-title" title="'. $microformatStartFormat .'"></span>';
$schedule .= '</span>' . ( $show_end_time ? $time_range_separator : '' );
$schedule .= '<span class="end-time dtend">';
$schedule .= ( $show_end_time ? tribe_get_end_date( $event, false, $time_format ) : '' ) . '<span class="value-title" title="'. $microformatEndFormat .'"></span>';
}
}
$schedule .= '</span>';
$schedule = $before . $schedule . $after;
return apply_filters( 'custom_tribe_events_event_schedule_details', $schedule );
}
@chugs
Copy link

chugs commented Aug 14, 2019

This code to modify the date format seems to work everywhere except the related posts.
The related posts on the single event show date details from the single event post rather than the related post.

Relevant code from related-events.php template:

<?php
if ( $post->post_type == Tribe__Events__Main::POSTTYPE ) {
    echo tribe_events_event_schedule_details($post);
 }
?>

I have tried using $post->ID but it doesn't seem to work.

Any idea why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment