11

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?

3
  • Also is this input or something? Will this be used by everyone? I ask because why not just create the date? Then it can be any formatting you want. Otherwise, as you already have guessed regex is the best way. It can still be an invalid that though. Leap year and what not. Commented Aug 17, 2011 at 21:15
  • In this case I'm going to repeat a famous quote: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -Jamie Zawinski, i feel this is one of those cases. Commented Aug 17, 2011 at 21:22
  • @Matt: regex tags were added by me, not by kaspernov Commented Aug 17, 2011 at 21:45

7 Answers 7

21

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/
Sign up to request clarification or add additional context in comments.

Comments

20

How to check string's date format in PHP?

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

2 Comments

Why isn't this higher on the list?
Failed below case $myString = '19-12-09 12:31:30'; if (DateTime::createFromFormat('Y-m-d G:i:s', $myString) !== FALSE) { echo 'true'; }
18

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.

2 Comments

Y-m-m: 2012-05-12... May the Decemberth of 2012.
Much more elegant than a regular expression in my book.
3
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?).

1 Comment

Thanks this solved my problem without having to add another question.
1

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;
}
?>

Comments

0

You could always just force it:

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

Comments

-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.