如何在 PHP 中检查字符串的日期格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7099481/
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-26 02:00:32  来源:igfitidea点击:

How to check string's date format in PHP?

phpregexpreg-match

提问by kaspernov

I would like to check if the string has this time format:

我想检查字符串是否具有这种时间格式:

Y-m-d H:i:s

and if not than do some code e.g.

如果不是,那么做一些代码,例如

if here will be condition do { this }
else do { this }

How to do this condition in PHP?

如何在 PHP 中执行此条件?

回答by Shad

preg_matchis what you are looking for, specifically:

preg_match是你正在寻找的,特别是:

if(preg_match('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/',$date)){
   //dothis
}else{
   //dothat
}

if you REALLY ONLY want properly formatted date, then

如果你真的只想要格式正确的日期,那么

/\d{4}-[01]\d-[0-3]\d [0-2]\d:[0-5]\d:[0-5]\d/

回答by cwallenpoole

You don't. It is impossible to tell if it is Y-m-dor Y-d-m, or even Y-d-dvs Y-m-m. What is 2012-05-12? May 12th or Dec. 5?

你没有。无法判断它是Y-m-dY-d-m,甚至是Y-d-dvs Y-m-m。什么是2012-05-12?5 月 12 日还是 12 月 5 日?

But, if you are content with that, you can always do:

但是,如果您对此感到满意,您可以随时执行以下操作:

// convert it through strtotime to get the date and back.
if( $dt == date('Y-m-d H:i:s',strtotime($dt)) )
{
    // date is in fact in one of the above formats
}
else
{
    // date is something else.
}

Though you might want to see if preg_match('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/',$date)isn't faster on this. Haven't tested it.

虽然你可能想看看preg_match('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/',$date)在这方面是否更快。没测试过

回答by Surendra Kumar Ahir

How to check string's date format in PHP?

如何在 PHP 中检查字符串的日期格式?

 if (DateTime::createFromFormat('Y-m-d G:i:s', $myString) !== FALSE) {
 echo 'true';
}                                                               

回答by Marc B

if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $yourdate)) {
   // it's in the right format ...
} else {
  // not the right format ...
}

Note that this only checks that the date string looks like a bunch of digits separated by colons and dashes. It does NOT check for oddities like '2011-02-31' (Feb 31st) or '99:99:99' for a time (99 o'clock?).

请注意,这只会检查日期字符串是否看起来像一堆由冒号和破折号分隔的数字。它不会在一段时间内(99 点钟?)检查诸如“2011-02-31”(2 月 31 日)或“99:99:99”之类的奇怪现象。

回答by CONvid19

From php.net

来自php.net

here's a cool function to validate a mysql datetime:

这是一个很酷的函数来验证 mysql 日期时间:

<?php
function isValidDateTime($dateTime)
{
    if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) {
        if (checkdate($matches[2], $matches[3], $matches[1])) {
            return true;
        }
    }

    return false;
}
?>

回答by AlienWebguy

You could always just force it:

你总是可以强制它:

date('Y-m-d H:i:s',strtotime($str));

回答by Oliver Charlesworth

The answer probably involves regular expressions. I suggest reading this documentation, and then coming back here if you're still having trouble.

答案可能涉及正则表达式。我建议您阅读此文档,如果您仍然遇到问题,请返回此处。