Reputation: 8836
is there a way for simpler and faster way to write this?
$currTime = date('H:i');
if ($currTime >= "12:09" && $currTime <= "16:08") {
echo rand(260, 272);
} elseif ($currTime >= "16:09" && $currTime <= "18:08") {
echo rand(278, 286);
} elseif ($currTime >= "18:09" && $currTime <= "20:08") {
echo rand(293, 303);
} elseif ($currTime >= "20:09" && $currTime <= "23:38") {
echo rand(338, 359);
} elseif ($currTime >= "23:39" && $currTime <= "23:59") {
echo rand(293, 302);
} elseif ($currTime >= "01:06" && $currTime <= "02:08") {
echo rand(195, 210);
} elseif ($currTime >= "02:09" && $currTime <= "02:23") {
echo rand(168, 179);
} elseif ($currTime >= "02:24" && $currTime <= "07:08") {
echo rand(121, 128);
} elseif ($currTime >= "07:09" && $currTime <= "09:08") {
echo rand(143, 160);
} elseif ($currTime >= "09:09" && $currTime <= "12:08") {
echo rand(187, 207);
} else {
echo rand(233, 241);
}
Upvotes: 0
Views: 114
Reputation: 23
Take my solution) This shortens and simplifies the code..
<?
function plan($a, $b, $c, $d)
{
$t = date('H:i');
if ($a <= $t && $t <= $b) { echo rand($c, $d); }
}
plan("12:09", "16:08", 260, 272);
plan("16:09", "18:08", 278, 286);
plan("18:09", "20:08", 293, 303);
plan("20:09", "23:38", 338, 359);
plan("23:39", "23:59", 293, 302);
plan("00:00", "01:05", 233, 241);
plan("01:06", "02:08", 195, 210);
plan("02:09", "02:23", 168, 179);
plan("02:24", "07:08", 121, 128);
plan("07:09", "09:08", 143, 160);
plan("09:09", "12:08", 187, 207);
?>
Upvotes: 0
Reputation: 95101
Don't look at faster way ... Look at proper ways ....
date
or time
like strings
mt_rand
instead of rand
functions
or object
rather than long switch or if else Example :
$currTime = new DateTime();
$range = [
new TimeRange("12:09-16:08", "260-272"),
new TimeRange("16:09-18:08", "278-286"),
new TimeRange("18:09-20:08", "293-303"),
new TimeRange("20:09-23:38", "338-359"),
new TimeRange("23:39-23:59", "195-210"),
new TimeRange("01:06-02:23", "168-179"),
new TimeRange("02:24-07:08", "121-128"),
new TimeRange("07:09-09:08", "143-160"),
new TimeRange("09:09-12:08", "187-241")
];
foreach($range as $timeRange) {
if ($timeRange->inRange($currTime)) {
printf("Current Time\t: %s\n", $currTime->format("H:i"));
printf("Range Time\t: %s\n", $timeRange->getRange());
printf("Random Value\t: %s\n", $timeRange->getRandom());
break;
}
}
Output
Current Time : 01:53
Range Time : 01:06 - 02:23
Random Value : 168
Used Class
class TimeRange {
private $timeFrom, $timeTo;
private $numFrom, $numTo;
private $format;
function __construct($time, $number, $format = "H:i") {
list($timeFrom, $timeTo) = explode("-", $time);
list($this->numFrom, $this->numTo) = explode("-", $number);
$this->timeFrom = DateTime::createFromFormat($format, $timeFrom);
$this->timeTo = DateTime::createFromFormat($format, $timeTo);
$this->format = $format;
}
function inRange(DateTime $currTime) {
return $currTime >= $this->timeFrom && $currTime <= $this->timeTo;
}
function getRandom() {
return mt_rand($this->numFrom, $this->numTo);
}
function getRange() {
return sprintf("%s - %s", $this->timeFrom->format($this->format), $this->timeTo->format($this->format));
}
}
Upvotes: 3
Reputation: 11328
$currTime = date('H:i');
$arr=array
(
"12:09,16:08"=>'260,272',
"16:09,18:08"=>'278, 286',
"18:09,20:08"=>'293, 303',
"20:09,23:38"=>'338, 359',
"23:39,23:59"=>'293, 302',
"01:06,02:08"=>'195, 210',
"00:00,00:30" =>'1,10'
//etc,etc...
);
foreach ($arr as $key=>$value) {
$key=explode(',',$key);
$value=explode(',',$value);
if( $currTime>= $key[0] && $currTime<=$key[1]) {
echo rand($value[0],$value[1]);
}
}
...however, i would keep OP's code as it is... :)
Upvotes: 0
Reputation: 14003
Check http://www.phpbench.com/
switch/case/default
VS. if/elseif/else
Control Structures | Total time
---------------------------------|-----------
if and elseif (using ==) | 190 µs
if, elseif and else (using ==) | 189 µs
if and elseif (using ===) | 158 µs
if, elseif and else (using ===) | 170 µs
switch / case | 200 µs
switch / case / default` | 228 µs
Conclusion:
Using a switch/case
or if/elseif
is almost the same. Note that the test is unsing ===
(is exactly equal to) and is slightly faster then using ==
(is equal to).
Upvotes: 0
Reputation: 490
$currTime = date('H:i');
$timevals=Array(
"23:39" => Array(293,302),
"20:09" => Array(338,359),
"18:09" => Array(293,303),
//... (etc, descending order)
);
foreach($timevals AS $time => $vals) {
if($curtime >=$time) {
echo rand($vals[0],$vals[1]);
break;
}
}
Upvotes: 3
Reputation: 7562
There is no cleaner way to do this, a switch statement will not work in this case. All you can do is make your if's a little shorter, but that's about it. I think your approach in this situation is good enough.
Upvotes: 0