lingo
lingo

Reputation: 1908

PHP detect if column value is DATE or DATETIME

Can I check with PHP if the given value is DATE or DATETIME format?

function decodeTimestamp($var)
{
    return date("d.m.Y H:i", strtotime($var));
}

What I'd like to do is following.

if $var == "2015-09-04" then return "4.9.2015"
if $var == "2015-09-04 14:00:00" then return "4.9.2015 14:00"

Upvotes: 1

Views: 205

Answers (1)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You can use preg_match as

function decodeTimestamp($var)
{
    if(preg_match('/\d{4}-\d{2}-\d{2}$/',$var)){
        return date("d.m.Y", strtotime($var));   
    }elseif(preg_match('/\d{4}-\d{2}-\d{2}\h\d{2}:\d{2}:\d{2}$/',$var)){
        return date("d.m.Y H:i", strtotime($var));   
    }
}

Upvotes: 2

Related Questions