简体   繁体   English

更改Laravel视图的出生日期格式

[英]Change date of birth format of view in laravel

Want to change date of birth format to dd/mm/yyy in laravel . 想要在laravel中将出生日期格式更改为dd / mm / yyy。
Actually i want it to be saved in dd/mm/yyyy format but it should be send to the database in yyyy/mm/dd format . 实际上,我希望将其保存为dd / mm / yyyy格式,但应以yyyy / mm / dd格式将其发送到数据库。 How can it be done ?? 如何做呢 ?? . Here is the code 这是代码

<div class="col-md-4">
                <div class="form-group">
                  <label>Date of Birth:</label>
                  <span class="data_fields data_personal">{{ date_to_indian($cust_data->dob) }}</span> 
                  <span class="edit_fields edit_personal"><input type="text" class="form-control" value="{{ $cust_data->dob }}" name="dob"></span>
                </div>
            </div>

Use Carbon's format() method: 使用Carbon的format()方法:

Carbon::parse($date)->format('d/m/Y');

If date is in Eloquent $dates property, just use format() : 如果date在Eloquent $dates属性中,则只需使用format()

$date->format('d/m/Y')

You can use laravel accessor mutators in laravel. 您可以在laravel中使用laravel访问器变异器。 Put below code in your model 将以下代码放入模型中

 protected $dates = [
    'dob',
];
//This method automatically save date Y-m-d format in database

public function setDobAttribute($date)
{
        $this->attributes['dob'] = Carbon::createFromFormat('d-m-Y', $date)->format('Y-m-d');
}

 //This method automatically fetch date d-m-Y format from database

public function getDobAttribute($date)
{
   return Carbon::createFromFormat('Y-m-d', $date)->format('d-m-Y');
}

You must be use carbon class namespace as following: 您必须使用carbon类名称空间,如下所示:

use Carbon;

试试这个代码:

<span class="edit_fields edit_personal"><input type="text" class="form-control" value="<?php echo date("d/m/Y",strtotime($cust_data->dob )); ?>" name="dob"></span>

You can use Date Mutators to convert a date to instances of Carbon , which extends the PHP DateTime class to provide an assortment of helpful methods. 您可以使用Date Mutators将日期转换为Carbon实例,这扩展了PHP DateTime类以提供各种有用的方法。

Add the following property in your model: 在模型中添加以下属性:

protected $dates = [
    'dob',
];

Then use it in your view as: 然后在您的视图中将其用作:

{{ $cust_data->dob->format('d/m/Y') }}

When retrieving attributes that are listed in your $dates property, they will automatically be cast to Carbon instances, allowing you to use any of Carbon's methods on your attributes 检索$dates属性中列出的属性时,它们将自动转换为Carbon实例,从而使您可以在属性上使用Carbon的任何方法

Use carbon date format method format() 使用碳日期格式方法 format()

{{ Carbon\Carbon::parse($cust_data->dob)->format('d-m-Y') }}

OR 要么

you can use PHP date function date() for it: 您可以为其使用PHP日期函数 date()

{{date('d-m-Y', strtotime($cust_data->dob))}}

Both are return same result but I recommend you to use Carbon because Carbon is inherited from PHP DateTime class. 两者都返回相同的结果,但是我建议您使用Carbon,因为Carbon是从PHP DateTime类继承的。

I think you can try twig filters here {{ $cust_data->dob | date('dm-Y') }} 我认为您可以在这里尝试使用树枝过滤器 {{ $cust_data->dob | date('dm-Y') }} {{ $cust_data->dob | date('dm-Y') }}

<div class="col-md-4">
                <div class="form-group">
                  <label>Date of Birth:</label>
                  <span class="data_fields data_personal">{{ $cust_data->dob | date('d-m-Y') }}</span> 
                  <span class="edit_fields edit_personal"><input type="text" class="form-control" value="{{ $cust_data->dob }}" name="dob"></span>
                </div>
            </div>

You can use $cust_data->dob->format('d/m/Y'); 您可以使用$cust_data->dob->format('d/m/Y'); for more options http://carbon.nesbot.com/docs/ 有关更多选项,请访问http://carbon.nesbot.com/docs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM