jquery datepicker – compare months of two dates

I am trying to compare the months of two dates using the jquery datepicker. I want it to check and see if my end date has the same month as my start date so that I can add in the end date month if needed:

Instead of displaying:

Read More

Event Dates

January 1st – 1st, 2017

March 3rd – 4th, 2017

I want to display:

Event Dates

January 1st – February 1st, 2017

March 3rd – 4th, 2017

This is the code I have so far. PS. I am using advanced custom fields within wordpress. That is why get_field(‘event_start_date’) pulls in the date. The only argument I really need help with is the <?php if ($endDate[M] != $startDate[M]): ?> code. That was just my best guess on how to do this but it does not work at all.

            <p>
              <?php if( get_field('event_start_date') ): ?>
                 <?php $startDate = DateTime::createFromFormat('Ymd', get_field('event_start_date')); ?>
                 <?php echo $startDate->format('F'); ?> <?php echo $startDate->format('jS'); ?> 
                    <?php if( get_field('event_end_date') ): ?>
                        <?php $endDate = DateTime::createFromFormat('Ymd', get_field('event_end_date')); ?>
                                                     - 
                        <?php if ($endDate[M] != $startDate[M]): ?>
                            <?php echo $endDate->format('F'); ?>
                        <?php endif; ?>
                            <?php echo $endDate->format('jS'); ?>, <?php echo $endDate->format('Y'); ?>
                    <?php else : ?>
                        , <?php echo $startDate->format('Y'); ?></span>
                    <?php endif; ?>
                <?php endif; ?>
            </p>

Related posts