Laravel and HTML 5 datetime component

I want to store all dates in UTC format into the database. To use HTML5 datetime-local element you have to do some conversion of datetimes. The HTML5 element expects a format of ‘2013-08-14T18:55’ (Y-m-d*H:i). In this post I’ll show you how I solved this.


View:

{{ Form::input('datetime-local', 'matchdate', $matchday->matchdateHTML5()); }}

Presenter:

public function matchdateHTML5() {
        return $this->toHTML5Date($this->resource->matchdate);
}

public function toHTML5Date($dt) {
        $dt->timezone('Europe/Amsterdam');
        return $dt->format('Y-m-d\TH:i');
}

Controller:

//update or create function:
$matchday['matchdate'] = $this->convertHtml5dateToUTC(Input::get('matchdate'));

public function convertHtml5dateToUTC($dt) {
        $matchdate = Carbon::createFromFormat('Y-m-d*H:i', $dt, 'Europe/Amsterdam');
        return $matchdate->tz('UTC');
}

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>