PHP之间的时间';s的问题——把我的头发拔出来


PHP between time's issue - pulling my hair out

我想知道是否有人能帮忙!我在这里拔头发。基本上,我从数据库中提取一些数据(开始和结束),它们都是日期时间列。我需要它做的是,如果开始是上午10:00,结束是下午12:00,我需要它将HTML类"y"添加到10,10,30,11,11:30&12.它在一个例子中确实有效,然后我在另一个例子上再次测试,每个TD都是类"n"。有人能告诉我们这里出了什么问题吗?

<?php
function betweenTime($input, $from, $till) {
    $fromTime = strtotime($from);
    $toTime = strtotime($till);
    $inputTime = strtotime($input);
    if(($inputTime >= $fromTime && $inputTime <= $toTime)) {
        return true;
    } else {
        return false;
    }
}
foreach ($Appts as $APK => $APV) {
    $StartI = settype(date('i', strtotime($Appts[$APK]['start'])), 'integer');
    $EndI = settype(date('i', strtotime($Appts[$APK]['end'])), 'integer');
    $Start = date('H:', strtotime($Appts[$APK]['start'])) . ($StartI >= 30 ? '30' : '00') . date(' a', strtotime($Appts[$APK]['start']));
    $End = date('H:', strtotime($Appts[$APK]['end'])) . ($EndI >= 30 ? '30' : '00') . date(' a', strtotime($Appts[$APK]['end']));
    $Start_ = date('H:i a', strtotime($Appts[$APK]['start']));
    $End_ = date('H:i a', strtotime($Appts[$APK]['end']));
    echo '
    <tr class="participant"> 
        <td class="pname"><div class="pname">James</div></td> 
        <td class="partTableCell' . ($Start === '08:00 am' || $End === '08:00 am' || betweenTime('08:00 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td> 
        <td class="partTableCell' . ($Start === '08:30 am' || $End === '08:30 am' || betweenTime('08:30 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td> 
        <td class="partTableCell' . ($Start === '09:00 am' || $End === '09:00 am' || betweenTime('09:00 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '09:30 am' || $End === '09:30 am' || betweenTime('09:30 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '10:00 am' || $End === '10:00 am' || betweenTime('10:00 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '10:30 am' || $End === '10:30 am' || betweenTime('10:30 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '11:00 am' || $End === '11:00 am' || betweenTime('11:00 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '11:30 am' || $End === '11:30 am' || betweenTime('11:30 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '12:00 pm' || $End === '12:00 pm' || betweenTime('12:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '12:30 pm' || $End === '12:30 pm' || betweenTime('12:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '01:00 pm' || $End === '01:00 pm' || betweenTime('01:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '01:30 pm' || $End === '01:30 pm' || betweenTime('01:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '02:00 pm' || $End === '02:00 pm' || betweenTime('02:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '02:30 pm' || $End === '02:30 pm' || betweenTime('02:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '03:00 pm' || $End === '03:00 pm' || betweenTime('03:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '03:30 pm' || $End === '03:30 pm' || betweenTime('03:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '04:00 pm' || $End === '04:00 pm' || betweenTime('04:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '04:30 pm' || $End === '04:30 pm' || betweenTime('04:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '05:00 pm' || $End === '05:00 pm' || betweenTime('05:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '05:30 pm' || $End === '05:30 pm' || betweenTime('05:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '06:00 pm' || $End === '06:00 pm' || betweenTime('06:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '06:30 pm' || $End === '06:30 pm' || betweenTime('06:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '07:00 pm' || $End === '07:00 pm' || betweenTime('07:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '07:30 pm' || $End === '07:30 pm' || betweenTime('07:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '08:00 pm' || $End === '08:00 pm' || betweenTime('08:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '08:30 pm' || $End === '08:30 pm' || betweenTime('08:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '09:00 pm' || $End === '09:00 pm' || betweenTime('09:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '09:30 pm' || $End === '09:30 pm' || betweenTime('09:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '10:00 pm' || $End === '10:00 pm' || betweenTime('10:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '10:30 pm' || $End === '10:30 pm' || betweenTime('10:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '11:00 pm' || $End === '11:00 pm' || betweenTime('11:00 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '11:30 pm' || $End === '11:30 pm' || betweenTime('11:30 pm', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>  
        <td class="partTableCell' . ($Start === '12:00 am' || $End === '12:00 am' || betweenTime('12:00 am', $Start_, $End_) ? ' y' : ' n') . ' dsep pok"></td>
        <td class="partTableCell n dsep pok"><img src="assets/images/delete.png" width="20" height="20" /></td>
    </tr>';
}
?>

如果您将$Appts数组定义为:,这对我来说很有效

$Appts = array(
    array('start' => '10:00', 'end' => '12:00')
);

但这不是一个好办法。对于这种工作,应该使用循环。并使用24小时时钟以方便:

<?php
$Appts = array(
        array('start' => '16:30', 'end' => '19:00')
    );
foreach ($Appts as $APK => $APV) {
    $tabStartH = 60 * 8; // Start at 8am
    $tabEndH = 60 * 24 - 30; // End at 11:30pm
    echo '<tr class="participant">';
    echo  '<td class="pname"><div class="pname">James</div></td>';
    $startH = hourToMinutes($APV['start']);
    $endH = hourToMinutes($APV['end']);
    echo $startH.' '.$endH."'r'n";
    for ($x = $tabStartH; $x < $tabEndH; $x += 30)
    {
        $test = minutesToHour($x); // Just for test printout
        echo '<td class="partTableCell' . ($x >= $startH && $x <= $endH ? ' y' : ' n') . ' dsep pok">' . $test . '</td>';
        echo "'r'n";
    }
    echo '<td class="partTableCell n dsep pok"><img src="assets/images/delete.png" width="20" height="20" /></td>';
    echo '</tr>';
}

function hourToMinutes($h)
{
    $parsed = date_parse($h);
    return $parsed['hour'] * 60 + $parsed['minute'];
}
function minutesToHour($m)
{
    $h = intval($m / 60);
    $i = $m % 60;
    return str_pad($h, 2, '0', STR_PAD_LEFT). ':'. str_pad($i, 2, '0', STR_PAD_LEFT);
}
?>

如果你的输入是12小时的格式,那么内部转换如下:

$start24 = date("G:i", strtotime($APV['start']));
$end24 = date("G:i", strtotime($APV['end']));
foreach ($Appts as $APK => $APV) {
    $fromTime = date("H:i",strtotime($Appts[$APK]['start']));
    $toTime = date("H:i",strtotime($Appts[$APK]['end']));
    $html ="...";
    for ($i=0;$i<=23;$i++){
      for ($j=0;$j<=30;$j+=30){
          $inputTime = str_pad($i,2,"0",STR_PAD_LEFT).":".str_pad($j,2,"0",STR_PAD_LEFT);
          $html .= "..."
                   .(($inputTime >= $fromTime && $inputTime <= $toTime)?'y':'n')
                   ."...";
      }
}
$html .="...";
print $html;