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() }}
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)
I have been trying to get the the Year and Month from the user and insert them into the database.
i am using input type month so that the user can send only the Year and the Month.
<input type="month" name="date_from"/>
<input type="month" name="date_to"/>
and this is my model
function setDateFromAttribute($value)
{
$this->attributes['date_from'] = \Carbon\Carbon::createDateFromFormat('Y-m', $value);
}
function setDateToAttribute($value)
{
$this->attributes['date_to'] = \Carbon\Carbon::createDateToFormat('Y-m', $value);
}
protected $fillable = [
'date_from',
'date_to',
];
and data is saved as 0000.00.00
the data type in my database for these two inputs is
timestamp
i do not know what i am doing wrong here. please help
I don't see createDateFromFormat as a valid Carbon method in the documentation https://carbon.nesbot.com/docs/
Likewise, there does not appear to be any createDateToFormat method.
Carbon::createFromFormat() returns a Carbon object, not a string or equivalent MySQL timestamp, so, assuming you change your code to be:
$this->attributes['date_from'] = \Carbon\Carbon::createFromFormat('Y-m', $value)->toDateTimeString();
It should provide the results you are looking for.
References:
Converting a carbon date to mysql timestamp.
#Ted Stresen-Reuter
Sorry for the late reply couldn't find a solution that works within the model. tried your method which i have tried before with minor changes.
thanks a lot for trying to help me.. i found a method to complete this inside the controller which i will not recommend, but this was the best i was able to find which works and i was on a tight schedule.
'date_from' => isset($date_from[$key]) ? date('Y-m-d h:i:s', strtotime($date_from[$key])) : '',
'date_to' => isset($date_to[$key]) ? date('Y-m-d h:i:s', strtotime($date_to[$key])) : '',
the type timestamp accepts at least dates in full formats. as i know how i pass the data from blade to the controller i was able to add a date method and within it a strttotime method to convert my YY-MM into YY-MM-DD and the date that is inserted by default will be 01.
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');
}
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');
}
I have an update user page in my webapp, and when I attempt to get information about a specific user, I get a data missing error as soon as I try to print out the created_at or updated_at columns. It works if I remove
<small>Oprettet: {{ date_format('%d%m%Y%h%m%s', $user->created_at) }} | Sist oppdatert: {{ date_format('%d%m%Y%h%m%s', $user->updated_at) }}</small>
Though as soon as I put it back in the error is back. I'm a little stuck here, thanks!
First of all: take a look at the documentation here.
You might want to remove the $this->timestamps attribute or set it to
protected $timestamps = ['created_at', 'updated_at', ...];
with your own timestamps.
Hope this helps for you!