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

发布于 2024-11-29 21:28:42 字数 192 浏览 0 评论 0原文

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

Y-m-d H:i:s

如果没有,则执行一些代码,例如

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

如何在 PHP 中执行此条件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ぶ宁プ宁ぶ 2024-12-06 21:28:42

preg_match 是您正在寻找的内容,具体来说:

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

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

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

preg_match is what you are looking for, specifically:

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/
夏日浅笑〃 2024-12-06 21:28:42

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

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

How to check string's date format in PHP?

 if (DateTime::createFromFormat('Y-m-d G:i:s', $myString) !== FALSE) {
 echo 'true';
}                                                               
公布 2024-12-06 21:28:42

你不知道。无法判断它是 Ymd 还是 Ydm,甚至是 YddYmm2012-05-12 是什么? 5月12日还是12月5日?

但是,如果您对此感到满意,您始终可以这样做:

// 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.
}

尽管您可能想看看 preg_match('/\d{4}-\d{2}-\d{2} \d{2} :\d{2}:\d{2}/',$date) 在此方面速度并不快。还没有测试过。

You don't. It is impossible to tell if it is Y-m-d or Y-d-m, or even Y-d-d vs Y-m-m. What is 2012-05-12? May 12th or Dec. 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.

聽兲甴掵 2024-12-06 21:28:42
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 ...
}

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

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?).

九歌凝 2024-12-06 21:28:42

来自 php.net

这里有一个很酷的函数来验证 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;
}
?>

From php.net

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

<?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;
}
?>
梦毁影碎の 2024-12-06 21:28:42

你总是可以强制它:

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

You could always just force it:

date('Y-m-d H:i:s',strtotime($str));
情感失落者 2024-12-06 21:28:42

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文