Damon Damon -4 years ago 94
PHP Question

Why this code places the - sign at the last

I am using below code to display advanced custom field (date fields) in wordpress. I want two dates to appear like e.g 12 mar - 15 mar 2017.

I am using this code

<?php echo the_sub_field('start_date').' - '.the_sub_field('end_date'); ?>


How to do it please explain ?

Right now I am getting result like this 18/09/201722/09/2017 -

my full html code

<table style=" border-collapse: collapse; table-layout: fixed; width: 800px;">
<tbody>
<tr>
<td style="border: 1px solid #ddd;"><h5 style="color:#044970;">Refernce</h5></td>
<td style="border: 1px solid #ddd;"><h5 style="color:#044970;">Dates</h5></td>
<td style="border: 1px solid #ddd;"><h5 style="color:#044970;">Venue</h5></td>
<td style="border: 1px solid #ddd;"><h5 style="color:#044970;">Fees</h5></td>
<td style="border: 1px solid #ddd;"><h5 style="color:#044970;">CPE Credits</h5></td>
<td style="border: 1px solid #ddd;"><h5 style="color:#044970;">Regsitrations</h5></td>
</tr>
</tbody>
</table>




<table style=" border-collapse: collapse; table-layout: fixed; width: 800px;">
<tbody>

<?php

if( have_rows('sub_seminars') ):

while( have_rows('sub_seminars') ) : the_row();

?>



<tr>
<td style="border: 1px solid #ddd;"><h6><?php the_sub_field('ref'); ?></h6></td>
<td style="border: 1px solid #ddd;"><h6><?php echo the_sub_field('start_date').' - '.the_sub_field('end_date'); ?></h6></td>
<td style="border: 1px solid #ddd;"><h6><?php the_sub_field('venue'); ?></h6></td>
<td style="border: 1px solid #ddd;"><h6><?php the_sub_field('fees'); ?></h6></td>
<td style="border: 1px solid #ddd;"><h6><?php the_sub_field('cpe_credits'); ?></h6></td>
<td style="border: 1px solid #ddd;"><a href="<?php home_url(); ?>/euromatechnew/seminar-registrations?seminartitle=<?php echo htmlentities(urlencode(get_the_title())) ?>&amp;seminarvenue=<?php echo the_sub_field('venue');
?>&amp;seminardates=<?php echo the_sub_field('start_date').the_sub_field('end_date'); ?>&amp;seminarref=<?php echo the_sub_field('ref') ?>" class="dt-btn" style="margin: 3px 0px 0px 0px">Register Now</a></td>
</tr>


<?php

endwhile;

endif;

?>




</tbody>
</table>

Answer Source

the_sub_field function already echoes data.

To get data and then echo it - use get_sub_field.

Proper code is:

<?php echo get_sub_field('start_date').' - '.get_sub_field('end_date'); ?>

Or as with your markup:

<td style="border: 1px solid #ddd;"><h6><?php the_sub_field('start_date');?> - <?php the_sub_field('end_date');?></h6></td>

To format your dates as you need you can use date fomatting functions of php, e.g date.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download