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 claire - September 18, 2014

Sure, this is how I'd do it:

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

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/