PHP關於時間的時段的重合、 整合的方法

來源:互聯網
上載者:User

標籤:php

<html >

<title></title>

<body>

<div>

 

<?php

     /**

     * 擷取到兩個重合時段的最大和最小

     * [get_min_max description]

     * @author jimswoo 20161016 <[<email address>]> 

     * @param  [type] $a [description]

     * @param  [type] $b [description]

     * @return [type]    [description]

     */

     function get_min_max($a,$b){

        $sort = array_merge($a,$b);

        array_multisort($sort);

        $end = array_pop($sort);

        return $sort[0].‘#‘.$end;//array($sort[0],$end);

    }


    /**

     * [is_repeat description]

     * 是否兩個時段重合

     * @author jimswoo 20161016 <[<email address>]> 

     * @param  [type]  $target  [description]

     * @param  [type]  $compare [description]

     * @param  boolean $run     [description]

     * @return boolean          [description]

     */

     function is_repeat($target,$compare,$run = true){

        $min = $compare[0];

        $max = $compare[1];

        $res = false;

        foreach($target as $v){

            if(($v >= $min && $v <= $max)) {

                $res = true;

                break;

            }

        }

        if($run && !$res){

          $res = is_repeat($compare,$target,false);

        }

        return $res;

    }


    /**

     * 把時段的值從字串轉為數組

     * @author jimswoo 20161016 <[<email address>]> 

     * [changeValue description]

     * @param  [type] $val [description]

     * @return [type]      [description]

     */

     function changeValue($val){

        $val = array_unique($val);

        $list = array();

        foreach($val as $v){

            $list[] = explode(‘#‘,$v);

        }

        return $list;

    }


    /**

     * [main_run description]

     * 比較方法

     * @author jimswoo 20161016 <[<email address>]> 

     * @param  [type] $all [description]

     * @return [type]      [description]

     */

     function main_run($all){

        $leng = count($all);

        $result = $un = array();

        $count = 0;

       for($i = 0;$i<$leng;$i++){

        for($j = $leng - 1;$j >= $i;$j--){

            if(is_repeat($all[$j],$all[$i])){

                if($j != $i){

                    $count++;

                }else{

                    $un[] = $all[$i][0].‘#‘.$all[$i][1];

                }

                $result[] = get_min_max($all[$j],$all[$i]);

                $all[$i] = $all[$j] = array(-3,-2);

                

            }else{

                $un[] = $all[$i][0].‘#‘.$all[$i][1];

            }

         }

          

        }

      

        $result = array_merge($result,$un);

      

        if($count == 0){

           $result = $all;

        }

        return array(‘c‘=>$count,‘v‘=>$result);

    }


    /**

     * [getComfirmTimes description]

     * @author jimswoo 20161016 <[<email address>]> 

     * @param  [type] $all [description] 格式array(array(2,4),array(34,332))

     * @return [type]      [description]

     */

     function getComfirmTimes($all){

        if(empty($all)){

            return array();

        }

        $c=0;

        do{

          $is_end = main_run($all);

          if($is_end[‘c‘] != 0){

              //var_dump($all);

              $all = changeValue($is_end[‘v‘]);

              

          }else{

             

              foreach($all as $k=>$v){

                  if($v[0] == -3){

                      unset($all[$k]);

                  }

              }

          }

          $c++;

        }while($is_end[‘c‘] != 0 && $c < 120);

        return $all;

    }


    $test = array(

    array(2,6),

    array(5,9),

    array(10,11),

    array(15,20),

    array(22,23),

    array(13,19)

    );

    $res = getComfirmTimes($test);

    print_r($res);

?>

</div>

</body>

</html>


本文出自 “12005322” 部落格,請務必保留此出處http://12015322.blog.51cto.com/12005322/1876592

PHP關於時間的時段的重合、 整合的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.