0

I have a string like this "oct 27 2015 11:11:22:933" coming from a MSSQL DB and I whould like to convert in a PHP datetime. if I do $date = new DateTime("oct 27 2015 11:11:22:933"); I receive the following error:

Failed to parse time string (oct 27 2015 02:24:24:967) at position 20 (:): Unexpected character'

Nowindi
  • 11
  • 2
  • Use [DateTime::createFromFormat()](http://www.php.net/manual/en/datetime.createfromformat.php) and specify the format to include the milliseconds – Mark Baker Oct 28 '15 at 09:32
  • Can you not just change the query that's running on the server so it doesn't bring back the milliseconds in the first place? No point retrieving them from the database if you're just going to throw them away in the PHP... – Matt Gibson Oct 28 '15 at 09:34

2 Answers2

0

You could try to use

date('H:i:s:u', $date);  

Notice that H is hour, s is second, i is minute, and u is microseconds.

Hope it helps.

-1

Acording to an answer provided here: https://stackoverflow.com/a/12039058/2160958

According to this documentation, all you need to do is prepend the timestamp with an @ character:

$timestamp = strtotime('oct 27 2015 11:11:22:933');
$dt = new DateTime('@' . $timestamp);
Community
  • 1
  • 1
vardius
  • 6,326
  • 8
  • 52
  • 97