Skip to content
Advertisement

How to hide 4 hours ahead in select option

 echo '<select required name="time" id="time" class="time">';
    $start = strtotime($this->helper->getOpeningTime());
    $end = strtotime($this->helper->getClosingTime());

    for ($calltime = $start; $calltime <= $end; $calltime = $calltime + 30 * 60) {
        $disabled = '';
        $time = date('H:i', $calltime);
        $time_with_am_pm = date('g:i a', $calltime);
        printf('<option class="time" value="%s" >%s</option>', $time, $time_with_am_pm);

    }
    echo '</select>';

This is the code I am using and

$start = strtotime($this->helper->getOpeningTime()); - 09:00

$end = strtotime($this->helper->getClosingTime()); - 18:00

I want to add two more steps: Let’s say a user comes at 10 am and try to pick a time slot I want to disable the time from 10 am – 2 pm whatever is in between this time need to be disabled is that possible?

If the current time is 10 am how do I disable 09:00 and 09:30

Desired output:

If the user comes at 10 am I want to disable 4 hours ahead so I want to disable 10:30, 11:00, 11:30, 12:00, 12:30 13:00, 13:30, 14:00 and 14:30

Advertisement

Answer

I have skipped old time and time from 10 am to 2 PM, Please check the below code

<?php

    echo '<select required name="time" id="time" class="time">';
    $start = strtotime('09:00');
    $end = strtotime('18:00');
    $notA = date($format, strtotime("$date + 4 hours"));
    for ($calltime = $start; $calltime <= $end; $calltime = $calltime + 30 * 60) {

        $disabled = '';
        $time = date('H:i', $calltime);
        $time_with_am_pm = date('g:i a', $calltime);
        if($calltime > time()+60*60*4)
        printf('<option class="time" value="%s" >%s</option>', $time, $time_with_am_pm);

    }
    echo '</select>';
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement