{"action":"created","issue":{"url":"https://api.github.com/repos/briannesbitt/Carbon/issues/693","repository_url":"https://api.github.com/repos/briannesbitt/Carbon","labels_url":"https://api.github.com/repos/briannesbitt/Carbon/issues/693/labels{/name}","comments_url":"https://api.github.com/repos/briannesbitt/Carbon/issues/693/comments","events_url":"https://api.github.com/repos/briannesbitt/Carbon/issues/693/events","html_url":"https://github.com/briannesbitt/Carbon/issues/693","id":162746489,"number":693,"title":"Carbon::instance fails if DateTime has microseconds assigned","user":{"login":"hackel","id":583677,"avatar_url":"https://avatars.githubusercontent.com/u/583677?v=3","gravatar_id":"","url":"https://api.github.com/users/hackel","html_url":"https://github.com/hackel","followers_url":"https://api.github.com/users/hackel/followers","following_url":"https://api.github.com/users/hackel/following{/other_user}","gists_url":"https://api.github.com/users/hackel/gists{/gist_id}","starred_url":"https://api.github.com/users/hackel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hackel/subscriptions","organizations_url":"https://api.github.com/users/hackel/orgs","repos_url":"https://api.github.com/users/hackel/repos","events_url":"https://api.github.com/users/hackel/events{/privacy}","received_events_url":"https://api.github.com/users/hackel/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-06-28T17:53:07Z","updated_at":"2016-07-07T22:03:03Z","closed_at":null,"body":"I'm using Carbon to work with MongoDB\\BSON\\UTCDateTime values, which are stored as a unix timestamp in milliseconds.  I want to convert this to a Carbon object and preserve the millisecond precision.  After converting it to a DateTime and trying to create a Carbon instance, it fails:\r\n```php\r\n$timestamp = floor(microtime(true) * 1000); // 1467132827636.0\r\n$mongoDate = new MongoDB\\BSON\\UTCDateTime($timestamp);\r\n$carbonDate = Carbon\\Carbon::instance($mongoDate->toDateTime());\r\n```\r\n> Exception with message 'DateTime::__construct(): Failed to parse time string (2016-06-28 16:53:47.636000000) at position 11 (1): The timezone could not be found in the database'\r\n\r\nThis seems to be caused because of the three extra trailing zeros generated by `$dt->format('Y-m-d H:i:s.u')`.  It's unclear to me why this is happening--it seems like it could be a bug in PHP (I'm running 7.0.8).  Certainly DateTime doesn't support nanosecond precision!  \r\n\r\nThe only way I've found to make this work is to manipulate the timestamps as strings (yuck):\r\n```php\r\n$timestamp = (string)$mongoDate; // \"1467132827636\"\r\nCarbon::parse(date('Y-m-d H:i:s', bcdiv($timestamp, 1000)) . '.' . substr($timestamp, -3));\r\n```"},"comment":{"url":"https://api.github.com/repos/briannesbitt/Carbon/issues/comments/231221094","html_url":"https://github.com/briannesbitt/Carbon/issues/693#issuecomment-231221094","issue_url":"https://api.github.com/repos/briannesbitt/Carbon/issues/693","id":231221094,"user":{"login":"malhayek2014","id":6562258,"avatar_url":"https://avatars.githubusercontent.com/u/6562258?v=3","gravatar_id":"","url":"https://api.github.com/users/malhayek2014","html_url":"https://github.com/malhayek2014","followers_url":"https://api.github.com/users/malhayek2014/followers","following_url":"https://api.github.com/users/malhayek2014/following{/other_user}","gists_url":"https://api.github.com/users/malhayek2014/gists{/gist_id}","starred_url":"https://api.github.com/users/malhayek2014/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/malhayek2014/subscriptions","organizations_url":"https://api.github.com/users/malhayek2014/orgs","repos_url":"https://api.github.com/users/malhayek2014/repos","events_url":"https://api.github.com/users/malhayek2014/events{/privacy}","received_events_url":"https://api.github.com/users/malhayek2014/received_events","type":"User","site_admin":false},"created_at":"2016-07-07T22:03:03Z","updated_at":"2016-07-07T22:03:03Z","body":"I had similar problem with MS SQL Server.\r\n\r\nAdd the following 2 methods to your model.\r\n\r\n\r\n\tpublic function getDateFormat()\r\n\t{\r\n\t    return 'Y-m-d H:i:s.u';\r\n\t}\r\n\t\t\r\n\tpublic function fromDateTime($value)\r\n\t{\r\n\t    return substr(parent::fromDateTime($value), 0, -3);\r\n\t}\r\n\r\nBut to avid having to copy and paste my code into bunch of model I used class inheritance and extended my model like so.\r\n\r\nI create my BaseModel Class\r\n\r\n\t<?php \r\n\tnamespace Vendor\\MyApp;\r\n\r\n\tuse Illuminate\\Database\\Eloquent\\Model as EloquentModel;\r\n\r\n\tclass BaseModel extends EloquentModel \r\n\t{\r\n\r\n\t\tpublic function getDateFormat()\r\n\t\t{\r\n\t\t\treturn 'Y-m-d H:i:s.u';\r\n\t\t}\r\n\r\n\t\tpublic function fromDateTime($value)\r\n\t\t{\r\n\t\t\treturn substr(parent::fromDateTime($value), 0, -3);\r\n\t\t}\r\n\r\n\t}\r\n\r\n\r\nThen for each model I had I extended my BaseModel class something like this\r\n\r\n\r\nclass User extends BaseModel \r\n{\r\n...\r\n...\r\n}\r\n"}}
{"actor":{"display_login":"malhayek2014"}}