Jump to content

PHP between time's issue - pulling my hair out


jamesmpollard

Recommended Posts

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?[/size]

 

-- I would like to point out here my indentation was all perfect but for some reason it's not when I post it lol.[/size]

 

<?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>';
}
?>
Edited by requinix
fixing code tags
Link to comment
Share on other sites

this should do what you are asking, without all the hard-coded logic (DRY programming), making it easier to modify any portion of the code or markup -

<?php

// if the current time period is between (inclusive) the start and end, return true
function betweenTime($time, $start, $end) {
    // strcmp -
    // greater or equal would be >= 0
    // less or equal would be <= 0
    if((strcmp($time,$start) >= 0 && strcmp($time,$end) <= 0)) {
        return true;
    } else {
        return false;
    }
}

// sample data
$Appts = array();
$Appts[] = array('start'=>'2015-12-22 08:00:00','end'=>'2015-12-22 08:00:00');
$Appts[] = array('start'=>'2015-12-22 10:00:00','end'=>'2015-12-22 12:00:00');
$Appts[] = array('start'=>'2015-12-22 13:00:00','end'=>'2015-12-22 13:30:00');

// list of times 8:00 am (08:00) to 12am (00:00) for the <td></td> grid and to test against
$times = array ( 0 => '08:00', 1 => '08:30', 2 => '09:00', 3 => '09:30',
 4 => '10:00', 5 => '10:30', 6 => '11:00', 7 => '11:30',
 8 => '12:00', 9 => '12:30', 10 => '13:00', 11 => '13:30',
 12 => '14:00', 13 => '14:30', 14 => '15:00', 15 => '15:30',
 16 => '16:00', 17 => '16:30', 18 => '17:00', 19 => '17:30',
 20 => '18:00', 21 => '18:30', 22 => '19:00', 23 => '19:30',
 24 => '20:00', 25 => '20:30', 26 => '21:00', 27 => '21:30',
 28 => '22:00', 29 => '22:30', 30 => '23:00', 31=> '00:00');

// loop over data
foreach($Appts as $arr){
    // get just the HH:MM of the 'YYYY-MM-DD HH:MM:SS' values
    $start = substr($arr['start'], 11, 5);
    $end = substr($arr['end'], 11, 5);
    
    echo '
    <tr class="participant">
    <td class="pname"><div class="pname">James</div></td>' . "\n";

    // output the time grid
    foreach($times as $time){
        $class = betweenTime($time,$start, $end) ? 'y' : 'n';
        echo '<td class="partTableCell ' . $class . ' dsep pok"></td>' . "\n";
    }

    echo '<td class="partTableCell n dsep pok"><img src="assets/images/delete.png" width="20" height="20" /></td>
    </tr>' . "\n\n";
}
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.