skip to Main Content

I save the user photo with Carbon in laravel 9

$user->photo_verified_at = Carbon::now()->timestamp;

But I get this output from database:

Error Message : ORA-01843: not a valid month
Position      : 42
Statement     : update "USERS" set "PHOTO_VERIFIED_AT" = :p0, "USERS"."UPDATED_AT" = :p1 where "ID" = :p2
Bindings      : [1672340645,2022-12-29 19:04:05,25]

If you a look at the data for created_at and updated_at in the database, they have the following format:

29/12/22 18:30:43,000000000

How is it possible to set Carbon to return in that format?

Carbon::now()->timestamp->format('dd/mm/yy H:i:s')

it returns

Error: Call to a member function format() on int

2

Answers


  1. You should be fine with just Carbon::now() or now() (it is a helper function calling Carbon::now())

    Login or Signup to reply.
  2. I am not exactly sure what you are storing there, if it is a timestamp that is literally a number, for example, 13102321 (just a made up number from me), it is the seconds that literally passed since a known date (I think it is January 1st, 1970 or something like that).

    So, if you exactly want that fomat, it is as easy as reading the documentation, it will state that you can use the normal PHP’s Date format:

    $formatedString = now()->format('d/m/Y H:i:s,u000');
    

    You have to add 3 more 0, as it has 9 0, that is nano seconds, super weird, but it is exactly the format you want…

    You can also try out just using now() or any method that returns a literal Carbon object, it can or cannot work, depends how you have your model’s attribute casted/declared.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search