0

有没有一种更简单、更快的方法来写这个?

$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);
}
4

6 回答 6

3
$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;
  }
}
于 2013-06-21T23:54:58.507 回答
3

不要看更快的方式……看正确的方式……

  • 从不比较datetime喜欢strings
  • 使用mt_rand而不是rand
  • 使用functionsorobject而不是 long switch 或 if else

例子 :

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

输出

Current Time    : 01:53
Range Time      : 01:06 - 02:23
Random Value    : 168

二手类

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));
    }
}
于 2013-06-21T23:55:43.307 回答
0

没有更干净的方法可以做到这一点,在这种情况下switch语句将不起作用。你所能做的就是让你的 if 更短一点,但仅此而已。我认为您在这种情况下的方法已经足够好了。

于 2013-06-21T23:50:58.243 回答
0

采取我的解决方案)这缩短并简化了代码..

<?
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);
?>
于 2013-06-21T23:54:42.020 回答
0
$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... :)
于 2013-06-22T00:28:35.887 回答
0

检查http://www.phpbench.com/

控制结构

switch/case/defaultVS。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

结论:

使用switch/caseorif/elseif几乎相同请注意,测试是 unsing ===(完全等于)并且比使用==(等于)稍微快一些。

于 2013-06-22T00:00:41.243 回答