如何使用 PHP 验证日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4978872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:25:31  来源:igfitidea点击:

how to validate date with PHP

php

提问by adam

If a date is submitted by form in following format, $month=2, $day=31, $year= 2010. How can i verify using PHP date function if it is valid date or not? Thanks.

如果按以下格式通过表单提交日期,则$month=2, $day=31, $year= 2010. 如何使用 PHP 日期函数验证它是否为有效日期?谢谢。

回答by Jonno_FTW

http://php.net/manual/en/function.checkdate.php

http://php.net/manual/en/function.checkdate.php

The checkdatefunction is the first result in google from the search "php validate date"

checkdate函数是谷歌搜索“php验证日期”的第一个结果

In your case, the usage would be:

在您的情况下,用法是:

checkdate($month, $day, $year);

回答by ahmeti

<?php
function validateDate($date, $format = 'Y-m-d H:i:s'){
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}
?>

var_dump(validateDate('2012-02-28 12:12:12')); # true
var_dump(validateDate('2012-02-30 12:12:12')); # false
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false

function was copied from this answeror php.net

函数是从此答案php.net复制的

回答by TuK

Try checkdate() http://php.net/manual/en/function.checkdate.php

试试 checkdate() http://php.net/manual/en/function.checkdate.php

checkdate($month, $day, $year);

returns true if date is valid / false otherwise

如果日期有效则返回真/否则返回假

回答by Gustavo Costa De Oliveira

bool checkdate ( int $month , int $day , int $year )

回答by Jon Hulka

Here's what I've come up with to combine the strictness of checkdate() with the convenience of DateTime (It converts entries like 'next thursday' or '2 weeks ago')

这是我想出的将 checkdate() 的严格性与 DateTime 的便利性相结合的方法(它可以转换诸如“下周四”或“两周前”之类的条目)

If the input string is invalid, it returns false. Empty dates are returned as null, and non-empty dates are formatted MySQL style 'Y-m-d'.

如果输入字符串无效,则返回 false。空日期返回为空,非空日期格式为 MySQL 样式“Ym-d”。

/**
 * @return variant null for empty date, mysql date string for valid date, or false for invalid date string
 */
function myCheckDate($date)
{
    $result=false;
    if($date=='')
    {
        $result=null;
    }
    else
    {
        //Best of both worlds
        // - flexibility of DateTime (next thursday, 2 weeks ago, etc)
        // - strictness of checkdate (2000-02-31 is not allowed)
        $m=false;
        $d=false;
        $y=false;
        $parts=date_parse($date);
        if($parts!==false)
        {
            $m=$parts['month'];
            $d=$parts['day'];
            $y=$parts['year'];
        }
        if($m!==false && $d!==false)
        {
            if($y===false) $y=date('Y'); //Default to this year
            //Try for a specific date - this catches bad entries like 'feb 31, 2000'
            if(checkdate($m,$d,$y)) $result=sprintf('%04d-%02d-%02d',$y,$m,$d);
        }
        else
        {
            //Try for something more generic - this allows entries like 'next thursday'
            $dt=false;
            try{ $dt=new \DateTime($date); }catch(\Exception $e){ $dt=false; }
            if($dt!==false) $result=$dt->format('Y-m-d');
        }
    }
    return $result;
}