Created
January 21, 2016 07:20
-
-
Save RandomArray/75c07c1a5a2eb5085216 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Sample PHP function that Returns True if the Current Time is between the Start and End hours.. | |
$ban_start = 19; // Clients not allow to login starting this hour | |
$ban_end = 7; // Clients allowed to log back in starting this hour | |
$currentHour = date('H'); | |
// Checking the normal way... | |
echo 'TEST 1: '; | |
if( clientBannedHours($currentHour, $ban_start, $ban_end) ){ | |
echo showTime(1); | |
}else{ | |
echo showTime(0); | |
} | |
// Combining it all works the same way.. | |
echo 'TEST 2: '.showTime( clientBannedHours($currentHour, $ban_start, $ban_end) ); | |
for($i=0;$i<=24;$i++){ | |
if(clientBannedHours($i,$ban_start,$ban_end)){ | |
echo numToHour($i).' - BANNED'.PHP_EOL; | |
}else{ | |
echo numToHour($i).' - OPEN TO BROWSE'.PHP_EOL; | |
} | |
} | |
// Returns true if $hour is between $ban_start && $ban_end | |
function clientBannedHours($hour, $ban_start, $ban_end){ | |
$currentHour = (int)date('H'); | |
if($hour >= $ban_start || $hour < $ban_end){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
// Turns 2 into 02:00 or 12 into 12:00.. Pads zeros.. | |
function numToHour($num){ | |
return (strlen($num)<=1) ? '0'.$num.':00' : $num.':00'; | |
} | |
// Displays the time language depending on true or false passed in | |
function showTime($bool){ | |
$r = 'TIME IS '.date('H:i').' - '; | |
$r .= ($bool) ? 'SHOULD BE BANNED' : 'OK TO CONTINUE'; | |
return $r.PHP_EOL.'---------------'.PHP_EOL;; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment