PHP获取每个preg_match的日期时间

I want to get the DateTime from a string in the format 'YYYY-MM-DD hh:mm:ss.

To clearify: I'm getting a string from an API, by now the string looks like this:

{ts '2014-09-02 14:42:49'}

But the string could be changed, so I won't just shorten the string to only the DateTime That's why i want to receive it via a RegEx. Because then the string can change as much as it wants, as long as the Date Format stays the same I'll always get the right result.

I found this question with a solution and tried to use it for my case. The RegEx is this:

$regEx = "(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})";

I also tried:

$regEx = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})";

It's working on regexr.com, but I can't get it to work on my PHP code.

The whole code is this:

        $result = array();
        $regEx = "(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})";
        preg_match($details['date'], $regEx, $result);
        var_dump($result);

But the var_dump delivers an empty array.

How must i define the RegEx so it's working with preg_match?

PHP regex needs delimiters, and also the order of the parameters of preg_match is wrong.

$regEx = '/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/';
preg_match($regEx, $details['date'], $result);
var_dump($result);

If $details['date'] is just the datetime format string, you may consider using DateTime class instead.

$datetime = new DateTime($details['date']);