dqsong2010 2018-09-22 19:06
浏览 21
已采纳

计时和安排php条件

I have an array like

            $array = (
                0 => array(
                    "start" => "Mon 09:30",
                    "end" => "Mon 11:00"
                ) ,
                1 => array(
                    "start" => "Sun 14:10",
                    "end" => "Sun 20:00"
                ) ,
                array(
                    "start" => "Sun 07:30",
                    "end" => "Sun 08:00"
                ));

Is there any idea to perform a function only if the current time is within this time.

example creation of array

If the current time is between start and end echo or return true. Date is not important

  • 写回答

2条回答 默认 最新

  • doujieluo5875 2018-09-23 07:05
    关注

    Creating datetime objects for each value in the time range subarrays is unnecessary overhead for your task. strtotime() gives you what you need and only what you need.

    As a matter of best practice, you should perform an early return as soon as you find a qualifying time range -- so your code is not performing wasteful iterations.

    Code: (Demo)

    $array = [
        ["start" => "Mon 09:30", "end" => "Mon 11:00"],
        ["start" => "Sun 14:10", "end" => "Sun 20:00"],
        ["start" => "Sun 07:30", "end" => "Sun 08:00"],
        ["start" => "Sun 08:30", "end" => "Sun 09:30"]
    ];
    
    function in_range($array) {
        $now = time();  // using server timezone
        echo date("D H:i") , "
    ";
        foreach ($array as $range) {
            if ($now >= strtotime($range["start"]) && strtotime($range["end"]) >= $now) {
                return $range;  // or true if you like
            }
        }
        return false;
    }
    
    var_export(in_range($array));
    

    Output (at the moment):

    Sun 09:02
    array (
      'start' => 'Sun 08:30',
      'end' => 'Sun 09:30',
    )
    

    If it is an impossibility for different daynames to occur in the same row of data, then you can further optimize your lookup process by removing the redundancies in your lookup array.

    If you restructure your day-time rows to be grouped by dayname and use that value as your associative first level key, you can use isset() to offer a quick return without any time comparisons and eliminate the chance of uselessly iterating all other days of the week.

    Code: (Demo)

    $array = [
        "Mon" => [
            ["start" => "09:30", "end" => "11:00"]
        ],
        "Sun" => [
            ["start" => "07:30", "end" => "08:00"],
            ["start" => "08:30", "end" => "09:30"],
            ["start" => "14:10", "end" => "23:59"]
        ]
    ];
    
    function in_range($array) {
        $dayname = date("D");
        if (!isset($array[$dayname])) {
            return false;  // quick, 1st level return
        }
        $now = date("H:i");
        foreach ($array[$dayname] as $range) {
            if ($now >= $range["start"] && $range["end"] >= $now) {
                return true;  // quick return
            }
        }
        return false;  // fallback return after iterating the dayname group
    }
    
    var_export(in_range($array));
    

    This may be premature optimization. I don't know the size and scope of your project. I just wanted to mention this in case a future researcher's project could benefit from it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM