控制器
static function show()
{
//
$output = '';
$result = DB::table('PersonalDetail')
->get();
foreach ($result as $key ) {
$dob = Carbon::$key->DOB;
// We need to compare the user's date of birth with today's date.
$now =Carbon::now();
// Calculate the time difference between the two dates.
$difference = $now->diff($dob);
// Get the difference in years, as we are looking for the user's age.
$age = $difference->y;
$output .= '<p>Age:-'.$age.' </p>';
}
return ($output);
}
查看刀片
调用字符串上的成员函数diff()(查看:/Applications/XAMPP/xamppfiles/htdocs/WedLaravel/WedLara/resources/views/pages/ViewPeo.blade.php)
我有这个错误,我已经尝试了很多
答案 0 :(得分:2)
要使用Carbon计算日期差异,您必须将两个日期都转换为Carbon对象,然后使用提供的方法:
$now = Carbon::now();
$dob = Carbon::createFromFormat('Y-m-d', $key->dob);
$diff = $now->diff($dob);
然后,您将拥有一个DateInterval对象,该对象将为您提供不同的间隔,因此您可以使用$d->y
。由于它是Carbon,您可以使用其他方法,例如
$now->diffInDays($dob);
$now->diffForHumans($dob);