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?

Recommended Answers

All 4 Replies

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++;
}

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)) { 
    // ...

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

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.