Converting date (with milliseconds) into timestamp

Tamilvanan picture Tamilvanan · May 28, 2016 · Viewed 13k times · Source

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.

Answer

Pyton picture Pyton · May 28, 2016

Use 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