2

I am using Laravel 5.1. I want to get milliseconds precision for created_at that I get from the database.

I have tried the solution given for this question. But there isn't any change. Also I tried raw query. Still I get only up to seconds. Is there any way to get millisecond precision?

Community
  • 1
  • 1
reshma
  • 672
  • 8
  • 24
  • You want to insert in to database or use for naming convention you can try Something like this " Carbon\Carbon::now()->timestamp" – Gautam Patadiya Feb 08 '17 at 06:55
  • i got data like "created_at": "2017-02-07 19:10:44" "updated_at": "2017-02-07 19:10:44" instead of "created_at": "2017-02-07 19:10:44.976" +"updated_at": "2017-02-07 19:10:44.976" – reshma Feb 08 '17 at 07:03
  • If you want time stamp then this can help you I am using it in laravel you have to try once – Gautam Patadiya Feb 08 '17 at 07:04
  • the thing is i wants it in millisecond precision in above format – reshma Feb 08 '17 at 07:13
  • Try this might can help you "$time = microtime(true); $mSecs = $time - floor($time); $mSecs = substr($mSecs,1); echo Carbon\Carbon::now()->format('d-m-Y H:i:s').$mSecs; " – Gautam Patadiya Feb 08 '17 at 07:25
  • I have to get the exact time that stored in my db. When i retrieve it from db, i got data after truncating the milliseconds. If i convert that data, i didn't get the milliseconds precision.What i get is some zero in that precision.Hope you get what i meant. – reshma Feb 08 '17 at 08:25
  • To store millisecond in database you need some different column format: Look this one http://stackoverflow.com/questions/26299149/timestamp-with-a-millisecond-precision-how-to-save-them-in-mysql – Gautam Patadiya Feb 08 '17 at 08:28
  • And this is also http://stackoverflow.com/questions/1459909/how-to-insert-time-2009-09-22-180937-881-in-mysql-my-column-type-is-datetime – Gautam Patadiya Feb 08 '17 at 08:30

1 Answers1

-2

It may be a problem about your column type. Assuming you are using MySQL, you should manually add (or modify) a column with type TIMESTAMP(3) on your table:

\DB::statement("ALTER TABLE `my_table` ADD COLUMN my_col TIMESTAMP(3) NULL");

More information here: https://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • I have tried many solution to insert created_at with millisecond precision. But what i get is only up to seconds. Then i manually edit my database and write trigger to update the column. But the problem arises when i try to retrieve it from db. – reshma Feb 08 '17 at 08:43
  • You have a problem on insert/get, but did you modify the **schema** of your table? (modify the column) What's the type of the column? – rap-2-h Feb 08 '17 at 08:51
  • yup. timestamp(3) – reshma Feb 08 '17 at 10:00
  • At inserting, make sure your eloquent model doesn't have the column name added to protected $dates. – aki Sep 12 '18 at 10:49