Store date (dd.mm.yyyy) as timestamp

Published 3 months 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.

The JavaScript part (I use a date picker) is working fine, but I can't get Laravel to store the correct timestamp:

// $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?

Thanks, Pida

Best Answer (As Selected By Pida)
AlbertMulaki

Hello, you can use eloquent mutators like this:

public function setEntryDateAttribute($date)
{
    $this->attributes['entry_date'] = (new Carbon($date));
}

For more you can reference here: https://laravel.com/docs/5.6/eloquent-mutators

tykus
tykus
3 months ago (659,440 XP)

You are probably getting the current UTC time by default. To mitigate this behaviour, you can precede the format with bang (!) to reset:

Carbon::createFromFormat('!d.m.Y', $request->entry_date);

or... use the startOfDay() method:

Carbon::createFromFormat('d.m.Y', $request->entry_date)->startOfDay();
petrit
Pida
Pida
3 months ago (17,400 XP)

@tykus There's no visible effect when using the bang or startOfDay(). Please note that the time is not an issue for me, I only need the correct date.

@petrit Could you be a little more explicit on how mutators can be used to store dd.mm.yyy formatted dates as timestamps?

AlbertMulaki

Hello, you can use eloquent mutators like this:

public function setEntryDateAttribute($date)
{
    $this->attributes['entry_date'] = (new Carbon($date));
}

For more you can reference here: https://laravel.com/docs/5.6/eloquent-mutators

Pida
Pida
3 months ago (17,400 XP)

@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?

AlbertMulaki

as the function says, it says createFromFormat()but i guess it wasn't the correct format provided by the front end, but by passing the date through the constructor, it will take the date in whatever format, and create a date from it.

Pida
Pida
3 months ago (17,400 XP)

I also tried new Carbon($date)inside the controller, so there must be some other reason...

AlbertMulaki

Hmm, i dont know then ahahham sorry :D

Please sign in or create an account to participate in this conversation.