How to check if an integer is within a range of numbers in PHP?

后端 未结 13 598
傲寒
傲寒 2020-12-04 10:57

How can I check if a given number is within a range of numbers?

相关标签:
13条回答
  • 2020-12-04 11:56

    I created a function to check if times in an array overlap somehow:

        /**
         * Function to check if there are overlapping times in an array of \DateTime objects.
         *
         * @param $ranges
         *
         * @return \DateTime[]|bool
         */
        public function timesOverlap($ranges) {
            foreach ($ranges as $k1 => $t1) {
                foreach ($ranges as $k2 => $t2) {
                    if ($k1 != $k2) {
                        /* @var \DateTime[] $t1 */
                        /* @var \DateTime[] $t2 */
                        $a = $t1[0]->getTimestamp();
                        $b = $t1[1]->getTimestamp();
                        $c = $t2[0]->getTimestamp();
                        $d = $t2[1]->getTimestamp();
    
                        if (($c >= $a && $c <= $b) || $d >= $a && $d <= $b) {
                            return true;
                        }
                    }
                }
            }
    
            return false;
        }
    
    0 讨论(0)
提交回复
热议问题