Changing the Modern Tribe Events Date Format

This is some code borrowed and extended on to change the Modern Tribes Event plugins date output format to make it a bit easier on the eye than the default output.

<?php // <~ don't add me in
add_shortcode( 'all_the_single_dates', 'all_the_single_dates' );
// Event Dates - display single date only for single dates & multi dates for multiple day events
function all_the_single_dates() {
// Get the start date and end date
// https://pastebin.com/ij0g9Tcf
// https://theeventscalendar.com/support/forums/topic/remove-end-date-if-same-as-start-date/#post-46651
ob_start();
$start_date = tribe_get_start_date(null, false,'d F');
$end_date = tribe_get_end_date(null, false, 'd F');
$start_date_day = tribe_get_start_date(null, false,'d');
$end_date_day = tribe_get_end_date(null, false, 'd');
$start_date_month = tribe_get_start_date(null, false,'F');
$end_date_month = tribe_get_end_date(null, false, 'F');
$start_date_year = tribe_get_start_date(null, false,'d F Y');
$end_date_year = tribe_get_end_date(null, false, 'd F Y');
// Only show the end date if it is different to the start date
if ($start_date !== $end_date) {
if ($start_date_month !== $end_date_month){
$start_date .= " &ndash; $end_date_year";
}
else {
$start_date = $start_date_day;
$start_date .= " &ndash; $end_date_year";
}
}
else {
$start_date = "$start_date_year";
}
?>
<p class="event-date"><?php esc_html_e($start_date) ?></p>
<?php
return ob_get_clean();
}
view raw functions.php hosted with ❤ by GitHub

It is a shortcode [all_the_single_dates] that outputs the dates like so ….

  • 09 – 14 September 2018
  • 28 October – 02 November 2018
  • 17 October 2018

So …

If the dates are in the same month it just outputs the month once.

If the dates cross into different months it outputs multi dates and corresponding months.

If the date is single it just outputs a single date.

If you wanted the month abbreviated swap the F’ with ‘M’

For other date manipulations see here and here.

2 Comments

  1. Stephen Page on September 14, 2018 at 2:08 pm

    “If you wanted the month abbreviated swap the ‘Y’ with ‘M’”

    I believe you meant “If you wanted the month abbreviated swap the ‘F’ with ‘M’”?

    • Neil Gowran on September 15, 2018 at 1:09 am

      Doh! – thanks – updated

Leave a Comment