Reputation: 1908
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
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