我正在尝试显示持续时间值,在 View 中使用数据库“列名”(“date_of_join”,“date_of_completion”)值
喜欢(1/1/2018 到 1/1/2019)这个值显示喜欢(1 年)
( 1/1/2018 到 1/2/2018 ) 值显示为 ( 1 month )
( 1/1/2018 到 10/2/2018 ) 值显示为 (1 个月 10 天 )
如何在 Laravel 中做到这一点 如何在 View 中显示 y=this
我的看法
@if(isset($details))
<table class="table table-striped">
<thead>
<tr>
<th>PHOTO</th>
<th>NAME</th>
<th>CERTIFICATE NO</th>
<th>COURSE</th>
<th>COURSE DURATION</th>
<th>DATE OF JOIN</th>
<th>CENTER NAME</th>
</tr>
</thead>
<tbody>
@foreach($details as $user)
<td>
<td>{{$user->student_name}}</td>
<td>{{$user->student_registration_id}}</td>
<td>{{$user->student_course}}</td>
<td>{{ }}</td>
<td>{{$user->date_of_join}}</td>
<td>{{$user->center_name}}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif(isset($message))
<p style="color:red" align="center">{{$message}}</p>
@endif
</div>
我的路线
Route::any( '/search', function(){
$id = Input::get( 'id' );
$certificate_issue_date = Input::get('certificate_issue_date');
if($id != ""){
$user = Student::where( 'student_registration_id', 'LIKE', '%' . $id . '%' );
if (!empty($certificate_issue_date))
{
$user->where('certificate_issue_date','=',$certificate_issue_date)->where('student_certificate_approval', 'APPROVED');
}
$user = $user->get();
if (count ( $user ) > 0)
return view( 'verify_certificate' )->withDetails( $user )->withQuery($id)->withDateOfJoin($certificate_issue_date);
}
return view ( 'verify_certificate' )->withMessage ( "No Details found!" );
} );
最佳答案
你可以通过 Mutators 来完成;
并使用Carbon
获取持续时间(例如:diffInSeconds、diffInDays、diffForHumans等)
如果你想要这样准确的格式,你需要写一个方法。
在你的学生模型中:
Class Student extend Model
{
...
public function getDurationAttribute() {
return Student::diffToHumanInterval($this->date_of_join, $this->date_of_completion);
}
public static function diffToHumanInterval($start, $end) {
$human = '';
$start_date = \Carbon\Carbon::parse($start);
$end_date = \Carbon\Carbon::parse($end);
$years = $end_date->diffInYears($start_date);
if ($years > 0) {
$human = $human.$years.' years ';
$start_date = $start_date->addYears(years);
}
$months = $end_date->diffInMonths($start_date);
if ($months > 0) {
$human = $human.$months.' months ';
$start_date = $start_date->addMonths($months);
}
$days = $end_date->diffInDays($start_date);
if ($days > 0) {
$human = $human.$days.' days ';
$start_date = $start_date->addDays($days);
}
return $human;
}
...
}
在你看来:
<td>{{ $user->duration }}</td>
关于php - 在 Laravel 中如何使用列连接日期和完成日期获取持续时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59170614/