php 使用 Laravel 5.2 从 Ymd 数据库中存储的日期计算年龄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35524482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:29:35  来源:igfitidea点击:

Calculate Age from date stored in database in Y-m-d using Laravel 5.2

phpdatabasedatelaravel-5.2

提问by sanainfotech

Hi User's add their DOB through the Form that store in database,

嗨,用户通过存储在数据库中的表单添加他们的 DOB,

I would like calculate age from stored date in the database which is in this format Y-m-d,

我想从数据库中存储的日期计算年龄,格式为 Ymd,

My Question is :

我的问题是:

  • How to calculate Age?

  • Where to put the logic , In Controller or Model?

  • How to pass the stored Date in view in this format 'm-d-Y'

  • How to pass the result of logic which is age in view.

  • I am using something as below in my model is this Right?

  • 如何计算年龄?

  • 在哪里放置逻辑,在控制器或模型中?

  • 如何以这种格式'mdY'在视图中传递存储的日期

  • 如何传递以年龄来看的逻辑结果。

  • 我在我的模型中使用如下内容,对吗?

This is controller:

这是控制器:

public function index()   {   
    $profile   = User::find($this->userid())->profiledetailsHasOne;  //This has Dob field                   
    return view('profile.index',['profile' => $profile ]); 
}

This is my Model:

这是我的模型:

public function getAge(){
    $this->birthdate->diff($this->attributes['dob'])
    ->format('%y years, %m months and %d days');
}

This is my View:

这是我的观点:

<tr>
    <th>Age</th>
    <td>{{$profile->getAge()}}</td>
</tr>

Is this Right? I am getting error as below

这是正确的吗?我收到如下错误

Call to a member function diff() on null

Call to a member function diff() on null

回答by Florent B.

Dates can be instances of Carbon, which provides an assortment of helpful methods.

日期可以是 Carbon 的实例,它提供了各种有用的方法。

In your model, import the Carbon class:

在您的模型中,导入 Carbon 类:

use Carbon\Carbon;

And define an accessor:

并定义一个访问器:

/**
 * Accessor for Age.
 */
public function getAgeAttribute()
{
    return Carbon::parse($this->attributes['birthdate'])->age;
}

You can then call ageas if it was a regular attribute. For example in a blade view:

然后,您可以age像调用常规属性一样调用它。例如在刀片视图中:

<p>{{ $user->age }} years</p>

回答by Gabriel Glauber

To show directly in your view:

要直接在您的视图中显示:

\Carbon\Carbon::parse($user->birth)->diff(\Carbon\Carbon::now())->format('%y years, %m months and %d days');

回答by Frederiek

Calculating the age in Laravel is best done using the build in Carbon. Dates returned in Laravel are already in a Carbon format.

在 Laravel 中计算年龄最好使用 Carbon 中的构建来完成。Laravel 中返​​回的日期已经是 Carbon 格式。

This logic should go in the model as a default getter for your model.

此逻辑应作为模型的默认 getter 进入模型。

public function getAge(){
    $this->birthdate->diff(Carbon::now())
         ->format('%y years, %m months and %d days');
}

This will result in "23 years, 6 months and 26 days"

这将导致“23 年 6 个月零 26 天”

Checkout the http://carbon.nesbot.com/docs/docs for all the fun stuff you can do with it.

查看http://carbon.nesbot.com/docs/docs,了解您可以用它做的所有有趣的事情。

Assuming you are using models in your view and since you should make a getAge()function in that model.

假设您在视图中使用模型,并且您应该getAge()在该模型中创建一个函数。

You can call your model in the view as $user->getAge()

您可以在视图中调用您的模型 $user->getAge()

回答by sanainfotech

Thanks for all you suggestion.

谢谢你的所有建议。

That was easy and awesome with carbon.

这对碳来说既简单又很棒。

I added this code in Model:

我在模型中添加了这段代码:

public function age() {
return $this->dob->diffInYears(\Carbon::now());
 }

回答by Abdelkarim SEBTI

I added this code in my Model:

我在我的模型中添加了这个代码:

protected $appends = ['age'];

public function getAgeAttribute()
{
   return Carbon::parse($this->attributes['birthday'])->age;
}

回答by Imtiaz Khandoker

use App\User;
use Carbon\Carbon;

Route::get('/userage',function(){

    $user_info = User::find(1);
    // When  ( $table->date('birthday'); ) 
    $birthday = $user_info->birthday;
    $user_age = Carbon::parse($birthday)->diff(Carbon::now())->format('%y years, %m months and %d days');

    echo($user_age); 
});

回答by sanainfotech

I also manage to get change the date format 'Y-m-d' from database to passing date in this 'd-m-Y' in view .

我还设法将日期格式“Ymd”从数据库更改为在视图中的“dmY”中传递日期。

I place this code in model

我将此代码放在模型中

public function getDOB(){ 
return $this->dob->format('d-m-Y'); 
 }