0

I'm trying to make a comparison of two dates. When the date's the same i want the end date to not be displayed. For some reason the comparison doesn't work. The dates are being displayed as shortcodes.

Here's my code:

add_shortcode('tribe-start-date', 'tribe_start_date_shortcode');
function tribe_start_date_shortcode() {
  global $post;
  $start_date = tribe_get_start_date( $post->ID, false, 'd.m.Y' );
  return $start_date;
}

add_shortcode('tribe-end-date', 'tribe_end_date_shortcode');
function tribe_end_date_shortcode() {
  global $post;
  $end_date = tribe_get_end_date( $post->ID, false, 'd.m.Y' );
  if ($start_date != $end_date) {
    return (' - ' . $end_date );
  }
} 

output:
01.10.2020 - 01.10.2020
Alex
  • 13
  • 3
  • 1
    You don't share `$start_date` between the two functions, so when you use it in the second one, it is always null. You could pass it in as a parameter when you call the function. – droopsnoot Oct 22 '20 at 12:47
  • When comparing dates, it best to use unix timestamp - https://stackoverflow.com/questions/19070116/php-check-if-date-between-two-dates – beatnik Oct 22 '20 at 14:15

0 Answers0