Hey Everyone

Im having a lil trouble rightnow making this script work. What im trying to do is make a script that will change the bgcolor color to red if the Serving time is less than or equal to 1 hr left which is working now but in the long run while testing the script i found a bug which is if the current time is already 5min past the Serving time the bgcolor needs to turn to color orange. but its not working properly

Here's the script

 <?php 
    date_default_timezone_set('Asia/Manila');
    $current_time= "18:10";
    $serving_time="18:05";


    if (strtotime($current_time) >= (strtotime($serving_time) - 3600 ))
    {

    echo "<tr bgcolor='red'><td align='center'>".$rows['FO']." / ".$rows['FoName']."/ ".$rows['Branch']."</td><td align='center'>".$rows['RA']."</td><td>".date('j F, Y', strtotime($rows['SDate']))." / ".$rows['Time']."</td><td align='center'>".$rows['Vehicle']."</td><td align='center'>".$rows['Service']."</td><td width='12%'>".$rows['Name']."</td><td align='center'>".$rows['Room']."</td><td align='center'>".$rows['Flight']."</td><td width='18%'>".$rows['Destination']."</td><td width='15%'>".$rows['Via']."</td><td width='8%'>".$rows['Remarks']."</td></tr> "; 


    }
    else if (strtotime($current_time) > (strtotime($serving_time) - 3900 ))

    {

    echo "<tr bgcolor='orange'><td align='center'>".$rows['FO']." / ".$rows['FoName']."/ ".$rows['Branch']."</td><td align='center'>".$rows['RA']."</td><td>".date('j F, Y', strtotime($rows['SDate']))." / ".$rows['Time']."</td><td align='center'>".$rows['Vehicle']."</td><td align='center'>".$rows['Service']."</td><td width='12%'>".$rows['Name']."</td><td align='center'>".$rows['Room']."</td><td align='center'>".$rows['Flight']."</td><td width='18%'>".$rows['Destination']."</td><td width='15%'>".$rows['Via']."</td><td width='8%'>".$rows['Remarks']."</td></tr> ";

    }
    else
    {
    echo "<tr><td align='center'>".$rows['FO']." / ".$rows['FoName']."/ ".$rows['Branch']."</td><td align='center'>".$rows['RA']."</td><td>".date('j F, Y', strtotime($rows['SDate']))." / ".$rows['Time']."</td><td align='center'>".$rows['Vehicle']."</td><td align='center'>".$rows['Service']."</td><td width='12%'>".$rows['Name']."</td><td align='center'>".$rows['Room']."</td><td align='center'>".$rows['Flight']."</td><td width='18%'>".$rows['Destination']."</td><td width='15%'>".$rows['Via']."</td><td width='8%'>".$rows['Remarks']."</td></tr> ";
    }





    ?>

If you add an equal sign it will work, but at 18.06 will change to default again. Is this the expected behaviour?

else if (strtotime($current_time) >= (strtotime($serving_time) - 3900 ))

hi i tried putting = sign already but still no luck yes it is better if it will change back to default when the time is 18.06 but even the else statement is not working its always in red color

Still not having the right result

here's a more simplier script

<?php

date_default_timezone_set('Asia/Manila');
$current_time= "18:05";
$serving_time="18:00";
if (strtotime($current_time) >= (strtotime($serving_time) - 3600 ))
{
echo "red";
}
    else if (strtotime($current_time) >= (strtotime($serving_time) - 3900 ))
{
echo "orange ";
}
else
{
echo "default";
}

?>

I think you have to change 3900 to 3300, and change the second statment to compare cases between red and orange limits, here's an example:

<?php

date_default_timezone_set('Asia/Manila');

$current_time = "17:05";
$serving_time = "18:00";


echo "result: \t";
if(
    strtotime($current_time) <= (strtotime($serving_time) - 3600)
)
{
    echo 'red';
}
elseif(
    strtotime($current_time) <= (strtotime($serving_time) - 3300)
    &&
    strtotime($current_time) >= (strtotime($serving_time) - 3600)
)
{
    echo 'orange';
}
else
{
    echo 'blue';
}

echo PHP_EOL;
echo 'red limit: '."\t". date('G:i:s', strtotime($serving_time) - 3600) . PHP_EOL;
echo 'orange limit: '."\t". date('G:i:s', strtotime($serving_time) - 3300) . PHP_EOL;
echo PHP_EOL;

Then switch $current_time between:

$current_time = "17:00"; # red
$current_time = "17:05"; # orange
$current_time = "17:06"; # default
Member Avatar for diafol

Remember that php is server-side. So when you enter a new 'zone', the background colour won't change automatically. You probably need javascript to intitiate that, if that's what you need.

Member Avatar for Zagga

I may have read the question wrong but it seems there are 3 possible states.

Serving time is 18:00
If current time is between 17:00 and 18:00 echo red.
If current time is between 18:01 and 18:05 echo orange.
If current time is before 17:00 or after 18:05 echo default.

<?php
date_default_timezone_set('Asia/Manila');
$serving_time="18:00";
$current_time= "18:01";

if (strtotime($current_time) >= strtotime($serving_time . "- 60 minutes")
&& strtotime($current_time) <= strtotime($serving_time))
{
    echo "red";
}
elseif (strtotime($current_time) > strtotime($serving_time)
&& strtotime($current_time) <= strtotime($serving_time . "+ 5 minutes"))
{
    echo "orange";
}
else
{
    echo "default";
}
?>

This will give the following results:
$current_time = "16:59"; # default
$current_time = "17:00"; # red
$current_time = "18:00"; # red
$current_time = "18:01"; # orange
$current_time = "18:05"; # orange
$current_time = "18:06"; # default

Hi everyone just got back from a Goodnight sleep. thank you all for the effort and response to my SOS i really appreciate it so much...its working fine now using Zagga script..thank you all again:))

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.