echo `{{$result->created_at}}`
result is 2018-06-23 08:05:14
but I want to echo just date. not time
I mean echo only 2018-06-23 not 2018-06-23 08:05:14
It's a Carbon instance. The recommended option is simply
{{ $result->created_at->toDateString() }}
Carbon docs: https://carbon.nesbot.com/docs/#api-formatting
This is pretty simple, directly escape date to whichever format you want like in the example below
{{ date('Y-m-d', strtotime($result->created_at))}}
You may handle this on the PHP side by just using substr:
echo {{ substr($result->created_at, 0, 10) }}
There are also ways that you could handle the formatting on the MySQL side, e.g. by using DATE_FORMAT:
DATE_FORMAT(created_at, '%Y-%m-%d')
you can use laravel Accessor by put the following method inside your model:
public function getCreatedAtAttribute($date)
{
return $date->format('Y-m-d');
}
Related
I have created_at field. on my database, and I need to get the month name, for example;
...
{{\Carbon\Carbon::now()->monthName}}
...
So how can I do it for $value->created_at?
I tried some solutions but didn't get the month name.
created_at already a Carbon instance, so you can do that by :
{{ $value->created_at->format('F') }}
Or,
{{ \Carbon\Carbon::parse($value->created_at)->format('F') }}
F - A full textual representation of a month (January through
December)
M - A short textual representation of a month (Jan through Dec)
Translate to other language : You can use carbon to format your local language, as for Russian language :
#php
\Carbon\Carbon::setLocale('ru');
#endphp
{{ \Carbon\Carbon::parse($value->created_at)->translatedFormat('F') }}
// output : ноябрь
you can try as created_atalready a Carbon instance, so you can use this
{{ $value->created_at->monthName }}
make sure $value is a instance of Illuminate\Database\Eloquent\Model;
if it is not a instance of laravel model then you can do like this
{{ \Carbon\Carbon::parse($value->created_at)->monthName }}
you can write by
{{ date('F', strtotime($value->created_at)) }}
you can get the name of month by the following code:
{{ date("F", strtotime($value->created_at)) }}
you can get more details from (php website)
Is there an option in Laravel Nova to display an readable date-time output and/or limit the output?
For example to : 29. October 2018 / 11. November 2018, 12:10 am
Code:
DateTime::make('Start')
->rules('required')
->sortable(),
As per documentation https://nova.laravel.com/docs/1.0/resources/fields.html#datetime-field
use Laravel\Nova\Fields\DateTime;
DateTime::make('Start')->format('DD MMMM YYYY'),
Must use Moment.js formatting rules https://momentjs.com/docs/#/displaying/format/
This is achieved in Nova 4.0+ with the displayUsing method:
DateTime::make('Updated', 'updated_at')
->displayUsing(fn ($value) => $value ? $value->format('D d/m/Y, g:ia') : '')
use this, hope it will works
Date::make('start')->format('F j, Y'),
We also faced such a problem.
This solution DateTime::make('Start')->format('DD MMMM YYYY'), helps only for index page, but didn't help for Edit page.
I don't know when this bug will be fixed in new Nova releases but we temporary used small hardcoding.
nova/src/Fields/Date.php
instead: return $value->format('Y-m-d');
use this one: return $value->format('m/d/Y');
nova/resources/js/components/Form/DateField.vue
In this vue component also should be changed a date format: dateFormat="m/d/Y".
nova/resources/js/components/Form/DateField.vue
For placeholder method use this one:
return this.field.placeholder || moment().format('MM/DD/YYYY')
Instead this:
return this.field.placeholder || moment().format('YYYY-MM-DD')
Also You should use Mutator in Your App\Model class if you store data in the database in another format. Something like this:
public function setLastUsedAttribute($value){
$date = Carbon::createFromFormat('m/d/Y', $value);
$this->attributes['last_used'] = $date->format('Y-m-d');
}
There are few ways that I can do this using PHP but I could not find a way to do that using laravel specific way.
I have a time that is coming from database in below format: Y:M:s
ex: 05:15:00
This is what I want to do:
add 30 minutes to that date, according to above example result should be: 05:45:00
Below is my current code and I want to add 30min to $endTime:
//get database value according to selected date
$allocatedDateQuery = DB::table('appointments')->where('date', $request->date)->get();
//loop throug every record to get time
foreach ($allocatedDateQuery as $value) {
$time = $value->time;
$endTime = $time;
}
I just got a perfect solution from here.
Use Carbon extention to simply acheive that.
What you have to do is parse your time to Carbon object and then you can use addMinutes() to do that and then you can format() if you want:
foreach ($allocatedDateQuery as $value) {
$time = Carbon::parse($value->time);
$endTime = $time->addMinutes(30);
$allocateValidateMessage .= Carbon::parse($value->time)->format('H:i') . ' - ' . $endTime->format('H:i') . ' ';
}
Usually I use php's date, you can give this a try
Date("Y:M:s", strtotime("30 minutes", strtotime($value->time))
That is converting your time into a string, adding 30minutes to it and converting it to the date format of your desire
Since you said you are grabbing the date from the database I am assuming you are also using Eloquent to query from the database.
You can use Eloquent Mutator Method in your Database Modal Class to mutate the data like this:
public function getAppointmentsAttribute($value) {
return Date("Y:M:s", strtotime("30 minutes", strtotime($value->time)));
}
You can even add another attribute without mutating the original value using Attribute assignments as well. This method caches your query and reduces database calls. Since you do not need to run local loops on the record your code is much cleaner.
I have a date, that prints out as YYYY-MM-DD.
How can I print it out as DD--MM--YEAR, or even better in english: eg 5th May 2018
This is how the date is printed out:
<td>{{$expenses->date}}</td>
You have 3 ways to do this:
1) Using Laravel Model - Doesn't work well with dates below 1900. You can however fix this by making a few adjustments.
<td>{{$expenses->date->format('j F, Y')}}</td>
2) Using PHP strtotime - Doesn't work well with dates below 1900. You can however fix this by making a few adjustments.
{{ date('j F, Y', strtotime($expenses->date)) }}
3) Using Carbon - Works well with all dates so far
{{ \Carbon\Carbon::parse($expenses->date)->format('j F, Y') }}
This is a duplicate of change the date format in laravel view page you can easily doing this.
<td>{{ date('d-M-y', strtotime($expenses->date)) }}</td>
You can make use of date-mutators
Add date field to your model's dates array.
Expense.php
class Expense extends Model
{
protected $dates = ['date', 'another_date_field'];
//other stuff
}
In view file you can format the date field like,
{{ $expenses->date->format('d-m-Y') }} //01-05-2018
or
{{ $expenses->date->format('l jS \\of F Y h:i:s A') }} //Tuesday 1st of May 2018 01:04:00 PM
Try this:
date('d-m-Y', strtotime($user->from_date));
It will convert date into d-m-Y format.
Try this in blade. It will show nicely with <sup> tags. Know this is an old thread but adding since it took lots of time for me to find this and wishing it'll be easier to future users.
{!! htmlspecialchars_decode(date('j<\s\up>S</\s\up> F Y', strtotime('21-05-2020'))) !!}
In Laravel you can add a function inside app/Helper/helper.php like
function formatDate($date = '', $format = 'Y-m-d'){
if($date == '' || $date == null)
return;
return date($format,strtotime($date));
}
And call this function on any controller like this
$expenses['date'] = formatDate($date,'d-m-Y');
And in Blade you can write directly
<td>{{$expenses->date}}</td>
Hope it helps!
The Carbon library is really cool with date formatting in laravel. The topic of String Formatting will be very useful to you.
Use of carbon in blade php can be handled by this format:
{{ \Carbon\Carbon::now()->toDateString() }}
This is my syntax in blade file :
{{ $product->filters->where("filter_id","10") }}
And my output for this looks like :
{"3":{"id":7153,"product_id":"1","filter_id":"10","data":"Kajaria","created_at":null,"updated_at":null}}
And I want to extract only a particular data i.e. Kajaria.
I'm getting confused in writing syntax.
I had already tried these syntaxes :
{{ $product->filters->where("filter_id","10")->data }}
{{ $product->filters->where("filter_id","10")['data'] }}
Try something like this:
{{ $product->filters->where("filter_id","10")->first()->data }}
The thing is $product is a collection and you need to get property of just one object, so first() method returns first object from a collection which you can use.
What I understand is that you want to get a filter for a product. So, try this.
Product::with('filters')->where('filter_id', 10)->first()->data;