Search code examples
phpdatetimestampstrtotimemilliseconds

Converting date (with milliseconds) into timestamp


I have date format like '25 May 2016 10:45:53:567'.

I want to convert into the time stamp.

strtotime function returns empty.

$date = '25 May 2016 10:45:53:567';
echo strtotime($date); 
// returns empty

When I removed the milliseconds, it's working.

$date = '25 May 2016 10:45:53';
echo strtotime($date);
// returns 1464153353

Please sort out my issue. Thanks in advance.


Solution

  • Split string:

    $date = '25 May 2016 10:45:53:001';
    preg_match('/^(.+):(\d+)$/i', $date, $matches);
    echo 'timestamp: ' . strtotime($matches[1]) . PHP_EOL;
    echo 'milliseconds: ' . $matches[2] . PHP_EOL;
    // timestamp: 1464162353 
    // milliseconds: 001