Jump to content

$_GET from URL, Logical Operator Problem


firedealer

Recommended Posts

index.php?time=dark won't get the dark hour result from $_GET and operators or vice versa.

 

What went wrong with this?

 

$current_time = date(G);
// light hours
if (($_GET['time'] == "light") || ($current_time >= 5 && $current_time <= 19)) {
echo "Light hours";
// dark hours
} elseif (($_GET['time'] == "dark") || ($current_time >= 20 && $current_time <= 23) || ($current_time >= 0 && $current_time <= 4)) {
echo "Dark hours";
}

Link to comment
https://forums.phpfreaks.com/topic/200087-_get-from-url-logical-operator-problem/
Share on other sites

So are you getting nothing or are you getting the wrong result?

 

Try adding echo $_GET['time'] to the top to make sure you really are getting passed what you think you're getting passed.  You might also try removing the second condition (checking $current_time) to make sure that it is indeed the $_GET condition that's breaking your logic.

That's because if condition for "light hours" is evalueated true, the condition for "dark hours" will not be evaluated at all.

Try like this:

 

if(isset($_GET['time'])) {
  if($_GET['time'] == 'light') {
     $hours = 'Light';
  } elseif ($_GET['time'] == 'dark') {
    $hours = 'Dark';
  } else {
    $hours = null;
  }
}

if(!isset($hours)) {
  $current_time = date('G');
  if ($current_time >= 5 && $current_time <= 19) {
    $hours = 'Light';
  } else {
    $hours = 'Dark';
  }
}

echo "$hours hours";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.