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条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗