簡體   English   中英

PHP 在時間問題之間 - 拉我的頭發

[英]PHP between time's issue - pulling my hair out

我想知道是否有人可以幫忙! 我在這里拔頭發。 基本上我從數據庫(開始和結束)中提取一些數據,它們都是日期時間列。 我需要它做的是,如果開始是說 10:00AM 和結束是 12:00PM 我需要它添加 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>';
}
?>
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;

好吧,如果你這樣定義 $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']));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM