PHP在时间问题之间 - 拉我的头发

时间:2015-12-22 23:23:33

标签: php

我想知道是否有人可以提供帮助!我把头发拉到这里。基本上我从数据库(开始和结束)中提取一些数据,这些数据都是日期时间列。我需要它做的是,如果开始是10:00 AM,结束是12:00 PM,我需要它来添加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>';
}
?>

2 个答案:

答案 0 :(得分:0)

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;

答案 1 :(得分:0)

如果您定义$ 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']));