Using if statement to hide date content

3 posts by 2 authors in: Forums > CMS Builder
Last Post: September 19, 2014   (RSS)

By JeffC - September 18, 2014

I am using the code below to display the start and end date of an event.

<p><?php echo date("j F Y", strtotime($article['event_starts'])) ?> - <?php echo date("j F Y", strtotime($article['event_finish'])) ?></p>

The code results in this: 18 September 2014 – 20 September 2014

Which is fine when the event runs over two or more days. However, it looks a bit daft when the start and end date is the same. ie: 21 September 2014 – 21 September 2014

Please could someone advise how I could omit the end date if it is the same as the start date.

Thanks

Jeff

By JeffC - September 19, 2014 - edited: September 19, 2014

Thank you.

I took your solution one stage further. 

<?php
$startdate = date("j F Y", strtotime($article['event_starts']));
$startdateabb = date("j", strtotime($article['event_starts']));
$enddate = date("j F Y", strtotime($article['event_finish']));
if($startdate != $enddate) : ?>
<p><?php echo $startdateabb ?> - <?php echo $enddate ?></p>
<?php else : ?><p><?php echo $startdate ?></p>
<?php endif; ?>

The resulting output looks like this if it is a one-day event: 19 September 2014

If it is an event that runs over two or more days it looks like this: 19 - 20 September 2014 

Jeff