【问题标题】:How to hide 4 hours ahead in select option如何在选择选项中隐藏提前 4 小时
【发布时间】:2020-04-17 12:06:45
【问题描述】:
 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>';

这是我正在使用的代码和

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

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

我想再添加两个步骤: 假设用户在上午 10 点到达并尝试选择一个时间段,我想禁用上午 10 点到下午 2 点之间的时间,这可能需要禁用吗?

如果当前时间是上午 10 点,我该如何禁用 09:00 和 09:30

期望的输出:

如果用户上午 10 点来,我想提前 4 小时禁用,所以我想禁用 10:30、11:00、11:30、12:00、12:30 13:00、13:30、14 :00 和 14:30

【问题讨论】:

  • 试着解释更多关于你想要的输出。您想查看没有 4 小时窗口的所有时间吗? 9.00, 9.30, 10.00, 02.00, .. 06.00?
  • No @AksenP 错了,我想很好,我想尝试展示这个 -> 我想采取两个步骤:如果用户上午 10 点来,我想隐藏过去的时间是 9:00 和 9:30(如果可以或可能的话,必须禁用它)。如果用户上午 10 点来,我想提前 4 小时禁用,所以我想禁用 10:30、11:00、11:30、12:00、12:30 13:00、13:30、14:00 和14:30

标签: php arrays sorting datetime


【解决方案1】:

我已经跳过了从上午 10 点到下午 2 点的旧时间和时间,请检查以下代码

<?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>';

【讨论】:

  • 我不能这样做你手动删除 $skip = array('10:00','10:30','11:00','11:30','12 :00','12:30','13:00','13:30','14:00');如果用户在上午 11 点来会发生什么?但我很欣赏你是如何做到这一点的
  • 我以为你想从你的问题中删除上午 10 点到下午 2 点以及所有过去的时间
  • 我以这个为例 -> 假设用户在上午 10 点到达并尝试选择一个时间段,我想禁用从上午 10 点到下午 2 点之间的时间需要的任何时间被禁用有可能吗?
  • 无论如何,这不是答案。 $date 在你的代码中做了什么?还有$notA$format
  • @jibingeorge 欢迎您,请您为我的回答点赞
【解决方案2】:

嗯,你需要one specific function

function roundUpToMinuteInterval($dateTime, $minuteInterval = 10)
{
    return $dateTime->setTime(
        $dateTime->format('H'),
        ceil($dateTime->format('i') / $minuteInterval) * $minuteInterval,
        0
    );
}

然后你可以转换每个时间点,这将帮助你得到这样的结果:

   // $s1 = new DateTime(); 
    $s1 = new DateTime(date('H:i', strtotime($came_at)));   // should be comment
    echo 's1 = '.$s1->format("H:i").PHP_EOL.PHP_EOL;

   // $s2 = new DateTime(); 
    $s2 = new DateTime(date('H:i', strtotime($came_at)));   // should be comment
    $s2 = $s2->modify("+4 hour")->modify("+1 hour");       // +4 +1 
    echo 's2 = '.$s2->format("H:i").PHP_EOL.PHP_EOL;

    $start1 = date('H:i', strtotime($start));
    $end1 = date('H:i', strtotime($end));
    echo 'start1 = '.$start1.PHP_EOL.PHP_EOL;
    echo 'end1 = '.$end1.PHP_EOL.PHP_EOL;

    $s3 = new DateTime($start1);  
    $s4 = new DateTime($end1);        
    $tmp_time = $s1->format("H:i");
    // correction time   
    $tmp = roundUpToMinuteInterval($s1, 30);    

    if ($tmp->format("H:i") === $tmp_time) {
        $s1 = $s1->modify("+1 minutes");  
    } else { 
        $s2 = $s2->modify("-30 minutes"); 
    } 

然后你可以像下面这样使用while loop

    while ($s1 < $s2){

       //  echo $s1->format("H:i").PHP_EOL;
       //  echo $s2->format("H:i").PHP_EOL;

        if ($s1 <= $s4 && $s1 > $s3){

            $time1 = $s1->format("H:i").PHP_EOL;    // current time
            echo 'current time = '.$time1;

            $d = roundUpToMinuteInterval($s1, 30); // round minutes to the next half
            $time =  $d->format("H:i").PHP_EOL;
            echo 'upper-rounded time = '.$time;

            $time_with_am_pm = $d->format('g:i a').PHP_EOL.PHP_EOL;   // AM/PM time
            echo 'AM/PM time = '.$time_with_am_pm;   

         //   printf('<option class="time" value="%s" >%s</option>', $time, $time_with_am_pm);
        }

        $s1 = $s1->modify("+30 minutes");  // next iteration with next half-hour
    }

输出:

s1 = 10:00

s2 = 15:00

start1 = 09:00

end1 = 18:00

current time = 10:01
upper-rounded time = 10:30
AM/PM time = 10:30 am

current time = 11:00
upper-rounded time = 11:00
AM/PM time = 11:00 am

current time = 11:30
upper-rounded time = 11:30
AM/PM time = 11:30 am

current time = 12:00
upper-rounded time = 12:00
AM/PM time = 12:00 pm

current time = 12:30
upper-rounded time = 12:30
AM/PM time = 12:30 pm

current time = 13:00
upper-rounded time = 13:00
AM/PM time = 1:00 pm

current time = 13:30
upper-rounded time = 13:30
AM/PM time = 1:30 pm

current time = 14:00
upper-rounded time = 14:00
AM/PM time = 2:00 pm

current time = 14:30
upper-rounded time = 14:30
AM/PM time = 2:30 pm

Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 2018-01-16
    • 1970-01-01
    • 2023-02-06
    相关资源
    最近更新 更多