将日期(以毫秒为单位)转换为时间戳

Tam*_*n N 4 php timestamp date strtotime milliseconds

我有日期格式 '25 May 2016 10:45:53:567'.

我想转换成时间戳.

strtotime 函数返回空.

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

当我删除毫秒时,它正在工作.

$date = '25 May 2016 10:45:53';
echo strtotime($date);
// returns 1464153353
Run Code Online (Sandbox Code Playgroud)

请理清我的问题.提前致谢.

Pyt*_*ton 6

用途DateTime:

$date = DateTime::createFromFormat('d M Y H:i:s:u', '25 May 2016 10:45:53:000');
echo $date->getTimestamp();
// 1464165953

// With microseconds
echo $date->getTimestamp().'.'.$date->format('u');
// 1464165953.000000
Run Code Online (Sandbox Code Playgroud)


Pro*_*nev 3

分割字符串:

$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 
Run Code Online (Sandbox Code Playgroud)