Published 1 month ago by Pida
I need help with refactoring a form. There's one issue left: I'd like to change a date input to dd.mm.yyyy format as the app is in German.
// $request->entry_date is '10.12.2017' (December 10th, 2017) $request->entry_date = Carbon::createFromFormat('d.m.Y', $request->entry_date); // In DDB after update: 2010-12-20 17:00:00 // Expected after update: 2017-12-10 (plus whatever time Carbon adds, I just care about the date) $client->update($request->all());
The 17:00 in the timestamp makes me suspect Carbon interprets the third and fourth digits of the year as hour, but I checked with the docs: Y is year (four digits), m is month (2 digits), d ist day (2 digits).
What's wrong with my code?
You are probably getting the current UTC time by default. To mitigate this behaviour, you can precede the format with bang (
!) to reset:
or... use the
@AlbertMulaki Thank you, that works! But now I wonder why...
I thought a mutator is just a central place to say "Whenever someone tries to set
$date as entry date, set
new Carbon($date) instead". But that's just what I did manually inside my controller, even using specific functions to handle the format (I tried
Carbon::createFromFormat() as well as other approaches).
Where's the difference?