Want to change date of birth format to dd/mm/yyy in laravel .
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 . 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::parse($date)->format('d/m/Y');
If date is in Eloquent $dates
property, just use format()
:
$date->format('d/m/Y')
Try this code:
<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.
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
Use carbon date format method format()
{{ Carbon\Carbon::parse($cust_data->dob)->format('d-m-Y') }}
OR
you can use PHP date function date()
for it:
{{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.
I think you can try twig filters here {{ $cust_data->dob | date('d-m-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');
for more options http://carbon.nesbot.com/docs/
You can use laravel accessor mutators in 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:
use Carbon;