Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 422
Tamilvanan Avatar asked Dec 19 '22 16:12

Tamilvanan


2 Answers

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
like image 110
Pyton Avatar answered Jan 01 '23 12:01

Pyton


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 
like image 32
Prokhor Sednev Avatar answered Jan 01 '23 10:01

Prokhor Sednev