美文网首页
比较两个时间的差值

比较两个时间的差值

作者: Sen_森 | 来源:发表于2020-07-15 09:57 被阅读0次
        /**
         * Notes: 获取两个日期的差值
         * User: Sen
         * DateTime: 2020/7/15 9:56
         * Return:
         */
        public function DiffDate($date1, $date2) {
            if (strtotime($date1) > strtotime($date2)) {
                $ymd = $date2;
                $date2 = $date1;
                $date1 = $ymd;
            }
            list($y1, $m1, $d1) = explode('-', $date1);
            list($y2, $m2, $d2) = explode('-', $date2);
            $y = $m = $d = $_m = 0;
            $math = ($y2 - $y1) * 12 + $m2 - $m1;
            $y = round($math / 12);
            $m = intval($math % 12);
            $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;
            if ($d < 0) {
                $m -= 1;
                $d += date('j', mktime(0, 0, 0, $m2, 0, $y2));
            }
            $m < 0 && $y -= 1;
            return array($y, $m, $d);
        }
    

    相关文章

      网友评论

          本文标题:比较两个时间的差值

          本文链接:https://www.haomeiwen.com/subject/sjqdhktx.html