简体   繁体   中英

PHP between time's issue - pulling my hair out

I was wondering if anyone could help! I'm pulling my hair out here. Basically I pull some data down from the DB (start & end) which are both datetime columns. What I need it to do is if the start is say 10:00AM and the end is 12:00PM I need it to add the HTML class "y" to 10,10:30, 11, 11:30 & 12. It did work in one instance, then I tested it again on another example and every TD was class "n". Could someone shed some light on what's going wrong here?

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

Well it's working for me if you define $Appts array as so:

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

But that's not good way to do this. You should use loops for that kind of job. And use 24-hour clock for convenience:

<?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);
}
?>

If your input is in 12-hour format then convert it internally like so:

$start24 = date("G:i", strtotime($APV['start']));
$end24 = date("G:i", strtotime($APV['end']));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM