Home > database >  How to swap a date range (ex. Jan 1, 2022-Sept 30, 2022) for a text label ("choose date")
How to swap a date range (ex. Jan 1, 2022-Sept 30, 2022) for a text label ("choose date")

Time:12-23

I'm self-taught and trying to learn more—I thought I could easily figure this out, but everything I tried didn't work.

This date picker currently shows the range of available dates (for a tour) as the first item in the list. (So if there are three dates on a tour, April 1 2022, June 30 2022, and Jan 1 2023, the first item will display "April 2022 - Jan 2023). I want to change it to either just show the first available date or text such as "select your date."

(Screenshot attached. So I want to remove the first line that shows April 2021-Oct 2021 completely—so you'd just see the first item or have it say "Select your date," instead.)

 <?php
            if (have_rows('available_tour_dates')) :
                $dates = array();
                $index = 1;
                while (have_rows('available_tour_dates')) : the_row();
                    $dates_single[] = getPrettyDate(get_sub_field('start_date')).' &ndash; '.getPrettyDate(get_sub_field('end_date'));
                    $c = (get_sub_field('bookable'))? '' : ' disabled';
                    $dates[] = sprintf($optionFormat, $index, $c, getPrettyDate(get_sub_field('start_date')).' &ndash; '.getPrettyDate(get_sub_field('end_date')));
                    $index  ;
                endwhile;
                    if(count($dates)>1):
                    $listing = '<select ><option value="0">'.getDateRange()."</option>".
                                implode(' ',$dates).
                                '</select>';
                    echo sprintf($defaultFormat, 'dates', 'Dates:', $listing);
                    else:
                    echo sprintf($defaultFormat, 'dates', 'Dates:', implode(' ',$dates_single));
                    echo "<input class='sacred_date_single' value='".implode(' ',$dates_single)."' type='hidden'>";
                    endif;
            ?>
                <!-- <p><a  href="#">See All Dates</a></p> -->
            <?php
            else : ?>
                <?php echo '<!--no available dates-->'; ?>
            <?php endif; ?>

            <?php //TODO MODAL 
            ?>
            <div  id="availDates">
                <h4>Available Dates</h4>
            </div>

Honestly, I lost track of what I've tried - but it wound up either showing "choose date" ABOVE the drop-down list or giving me fatal errors. I think I should be changing this line, but I'm not sure where/how to add the "choose your date" text: $listing = ''.getDateRange()."".

TIA!

CodePudding user response:

Right after you set the value of index to 1 add $dates[] = 'Choose Date'; as shown in the snippet below.

$dates   = array();
$index   = 1;
$dates[] = 'Choose Date';
while (have_rows('available_tour_dates')) : the_row();
     $dates_single[] = getPrettyDate(get_sub_field('start_date')).'
  • Related