search
Homephp教程PHP源码对比日期与判断日期是否合法类

跳至

 $smallDate 返回 > 0
     * $bigDate < $smallDate 返回 < 0
     * $bigDate = $smallDate 返回 = 0
     *
     * @param $bigDate
     * @param $smallDate
     *
     * @returns
     */
    public static function dateCmp($bigDate, $smallDate) {
        if (!self::isDate($bigDate) || !self::isDate($smallDate)) {
            throw new Exception(&#39;参数不是合法的日期&#39;);
        }

        return strcmp($bigDate, $smallDate);
    }

    public static function isDate($dateTime, $checkTime = false) {
        $strArray = explode(&#39; &#39;, $dateTime);
        $date = $strArray[0];
        $time = $strArray[1];
        // 不是 dateTime 格式
        if (!$date || !$time) {
            return false;
        }

        $dateArray = explode(&#39;-&#39;, $date);
        $year = $dateArray[0];
        $month = $dateArray[1];
        $day = $dateArray[2];
        // 年在 1-9999 年
        if (!self::_checkLimit($year, 1, 9999)) {
            return false;
        }

        if (!self::_checkLimit($month, 1, 12)) {
            return false;
        }

        if (!self::_checkLimit($day, 1, 31)) {
            return false;
        }

        if ($checkTime) {
            $timeArray = explode(&#39;:&#39;, $time);
            $hour = $timeArray[0];
            $minute = $timeArray[1];
            $second = $timeArray[2];

            if (!self::_checkLimit($hour, 0, 24)) {
                return false;
            }
            if (!self::_checkLimit($minute, 0, 60)) {
                return false;
            }
            if (!self::_checkLimit($second, 0, 60)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 检查字符串是否在 start to 的区间内
     *
     * @param $str
     * @param $start
     * @param $to
     *
     * @returns
     */
    private static function _checkLimit($str, $start, $to) {
        if ($str < $start || $str > $to) {
            return false;
        }

        return true;
    }
}

                   

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.