I would like to know how to recuperate the format time on my form please? For example start_time => 20:00 and end_time 22:00.
In my table course I have this
public function up()
{
Schema::create('course', function (Blueprint $table) {
$table->increments('id');
$table->date('date_seance');
$table->time('start_time');
$table->time('end_time');
$table->timestamps();
});
}
Then, on my model course I have this
class Course extends Model
{
//
protected $dates = ['date_seance', 'start_time', 'end_time'];
}
In my view index.blade
@foreach($course as $item)
<tr>
<td> {{$item->date_seance->format('d/m/Y') }}</td>
<td> {{$item->start_time}}</td>
<td> {{$item->end_time}}</td>
Thank you for your help.
I don't think you can use 'start_time'
and 'end_time'
in date since they are not.
class Course extends Model
{
protected $dates = ['date_seance'];
}
Then use
<td> {{$item->date_seance->format('d/m/Y') }}</td>
<td> {{date('H:i', strtotime($item->start_time)) }}</td>
<td> {{date('H:i', strtotime($item->end_time)) }}</td>
the type of column is instance of Carbon class what about trying this :
<td> {{$item->date_seance->toDateString() }}</td>
You should try this:
<td> {{date('d/m/Y H:i', strtotime($item->date_seance)) }}</td>
try below format you already stored in time so no need to convert in strtotime
<td> {{date('H:i', $item->start_time) }}</td>
Mutators and Accessors are best choice to handle these cases. Because if we have to show same column(start_time) on multiple pages then we have to re-format the time each time on each page separately.
If you use Mutators and Accessors then you can easily format it on one place and call it where ever you want just as an attribute
Suppose you have a timestamp named as start_time
where you are saving time in format i.e. toTimeString() or H:i or h:i
.
If you want to show it in different format and save in toTimeString
then you will need the following things
setStartTimeAttribute
to save time in laravel formatAn Accessor getStartTimeAttribute
to show it on different pages with required format i.e. h:i or H:i
public function setStartDateAttribute($value)
{
$this->attributes['start_time'] = Carbon::parse($value)->format('H:i');
}
public function getStartDateAttribute()
{
return Carbon::parse($this->attributes['start_time'])->format('H:i');
}
Now you can access formatted time as $object->start_time
i.e. 20:00