gpt4 book ai didi

javascript - 如何将日期转换为年龄?

转载 作者:行者123 更新时间:2023-12-03 00:41:10 26 4
gpt4 key购买 nike

我想根据我存储在数据库中的Y-m-d格式计算年龄。如何传递dob的结果,即 View 中的年龄。如何以这种格式传递 View 中存储的日期,例如年龄20岁。

我的观点:

<th style="width: 10px;text-align: left">Age</th>

@if(count($members)>0)
@foreach($members as $key=>$member)
<tr>
<td>{{$key+1}}</td>
<td>{{$member['dob']}}</td>
@endforeach
@endif

我的 Controller :

 public function index()
{
$departments = [];

$members = Member::where('status', 0)
->leftjoin('member_details', 'members.cnic', '=', 'member_details.cnic')
->leftjoin('districts','districts.dis_id','=','members.district')
->paginate(100,['members.*','member_details.*','members.id as id','member_details.id as memberid','district','dis_name']);

for ($m = 0; $m <count($members); $m++) {
$finalDep = [];
$getDepartments = MemberInterestDepartment::where('cnic' , $members[$m]['cnic'])
->leftjoin('departments','departments.dep_id','=','member_interest_departments.department')
->get(['department','dep_name'])->toArray();
if(count($getDepartments)>1){

for($r=0; $r<count($getDepartments); $r++){
$finalDep[] = $getDepartments[$r]['dep_name'];
}
}else{
$finalDep = $getDepartments[0]['dep_name'];
}

$departments[] = array(
'id' => $members[$m]['id'],
'cnic' => $members[$m]['cnic'],
'name' => $members[$m]['name'],
'district' => $members[$m]['dis_name'],
'party_joining_year' => $members[$m]['party_joining_year'],
'dob' => $members[$m]['dob'],
'qualification' => $members[$m]['qualification'],
'profession' => $members[$m]['profession'],
'department' => $finalDep,
);
}
$allDepartments = Department::all();

return view('dashboard')->with(['pages' => $members,'members' => $departments, 'province' => $province, 'division' => $division, 'districts' => $districts, 'departments' => $allDepartments]);

}

最佳答案

首先导入 Controller

use Carbon\Carbon;

然后在parse中传递dob值

$dob = '1990/01/15';    
echo Carbon::parse($dob)->age .' years';

以下是具体操作方法:

$departments[] = array(
'id' => $members[$m]['id'],
'cnic' => $members[$m]['cnic'],
'name' => $members[$m]['name'],
'district' => $members[$m]['dis_name'],
'party_joining_year' => $members[$m]['party_joining_year'],
'dob' => Carbon::parse($members[$m]['dob'])->age .' years',
'qualification' => $members[$m]['qualification'],
'profession' => $members[$m]['profession'],
'department' => $finalDep,
);

关于javascript - 如何将日期转换为年龄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53456474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com