0

Needs to achieve such an effect:

$dateall=$year.$month.$day; 
$arr_from=array('20120401','20120416','20120510'); 
$arr_to=array('20120405','20120425','20120515'); 

if($dateall>=$arr_from[0] && $dateall<=$arr_to[0] || $dateall>=$arr_from[1] && $dateall<=$arr_to[1] || ...) 
{ 
    $calendar .= '<td class="reservation"><span title="reservation" id="id'.$year.$month.$day.'">'.$day.'</span></td>';   
} 
else if 
//other conditions

The above code works fine.
But I need to rewrite this code as:

if($dateall>=$arr_from[$j] && $dateall<=$arr_to[$j]) // something like a loop 
{ 
    $calendar .= '<td class="reservation"><span title="reservation" id="id'.$year.$month.$day.'">'.$day.'</span></td>'; 
...

Does anyone know how to do it?

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by blocblue
0

Try to.. Just put it to the while loop:
(If I understand, $j is something like index, (staring with 0, and getting bigger)right?
so do it like this:

$j = 0;
$count_to = count($arr_to);
$count_from = count($arr_from);
while($j !== $count_to && $j !== $count_from){
    if($dateall>=$arr_from[$j] && $dateall<=$arr_to[$j]) // something like a loop
    {
    $calendar .= '<td class="reservation"><span title="reservation" id="id'.$year.$month.$day.'">'.$day.'</span></td>';
    ...
}
$j++;
}
0

Wrap the loop in a function. E.g.

function is_date_between($date, $dates_from, $dates_to)
{
    $count = max(count($dates_from), count($dates_to));

    for($i = 0; $i < $count; $i++) {
        $date_from = isset($dates_from[$i]) ? $dates_from[$i] : false;
        $date_to = isset($dates_to[$i]) ? $dates_to[$i] : false;

        if($date >= $date_from && $date <= $date_to)
            return true;
    }

    return false;
}

$date = "{$year}{$month}{$day}"; 
$dates_from = array('20120401','20120416','20120510'); 
$dates_to = array('20120405','20120425','20120515'); 

if(is_date_between($date, $dates_from, $dates_to)) { 
    // ...

Edited by blocblue: n/a

0

blocblue, This is it! :)
But this second return false; must be removed - then work properly.
Very very Thank you!
desup also thank you!

0

I don't understand why the return false would cause an issue. That would be required if the date wasn't between one of the from / to dates.

Glad it solves your problem though. Please mark as solved :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.