php 更改 Laravel 视图页面中的日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40038521/
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
change the date format in laravel view page
提问by user3386779
I want to change the date format which is fetched from database.
now I got 2016-10-01{{$user->from_date}}
.I want to change the format 'd-m-y' in laravel 5.3
我想更改从数据库中获取的日期格式。现在我得到了 2016-10-01 {{$user->from_date}}
。我想在 laravel 5.3 中更改格式“dmy”
{{ $user->from_date->format('d/m/Y')}}
回答by Hamelraj
In Laravel use Carbonits good
在 Laravel 中使用Carbon它的好处
{{ \Carbon\Carbon::parse($user->from_date)->format('d/m/Y')}}
回答by Mayank Pandeyz
Try this:
尝试这个:
date('d-m-Y', strtotime($user->from_date));
It will convert date into d-m-Y
format.
它将日期转换为d-m-Y
格式。
Note:This solution is a general solution that works for php and any of its frameworks. For a Laravel specific method, try the solution provided by Hamelraj.
注意:此解决方案是适用于 php 及其任何框架的通用解决方案。对于 Laravel 特定的方法,请尝试Hamelraj提供的解决方案。
回答by HorecioDias
In your Model set:
在您的模型集中:
protected $dates = ['name_field'];
after in your view :
在您看来之后:
{{ $user->from_date->format('d/m/Y') }}
works
作品
回答by Marek Skiba
You can check Date Mutators
: https://laravel.com/docs/5.3/eloquent-mutators#date-mutators
您可以查看Date Mutators
:https: //laravel.com/docs/5.3/eloquent-mutators#date-mutators
You need set in your User
model column from_date
in $dates
array and then you can change format in $dateFormat
您需要在数组中的User
模型列from_date
中设置,$dates
然后您可以更改格式$dateFormat
The another option is also put this method to your User
model:
另一种选择也是将此方法放入您的User
模型中:
public function getFromDateAttribute($value) {
return \Carbon\Carbon::parse($value)->format('d-m-Y');
}
and then in view if you run {{ $user->from_date }}
you will be see format that you want.
然后在视图中,如果您运行,{{ $user->from_date }}
您将看到您想要的格式。
回答by Umar Tariq
回答by Naresh Kumar P
Method One:
方法一:
Using the strtotime()
to time is the best format to change the date to the given format.
使用strtotime()
to 时间是将日期更改为给定格式的最佳格式。
strtotime()
- Parse about any English textual datetime description into a Unix timestamp
strtotime()
- 将任何英文文本日期时间描述解析为 Unix 时间戳
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
该函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于 now 给出的时间戳,或如果现在没有提供当前时间。
Example:
例子:
<?php
$timestamp = strtotime( "February 26, 2007" );
print date('Y-m-d', $timestamp );
?>
Output:
输出:
2007-02-26
Method Two:
方法二:
date_format()
- Return a new DateTime object, and then format the date:
date_format()
- 返回一个新的 DateTime 对象,然后格式化日期:
<?php
$date=date_create("2013-03-15");
echo date_format($date,"Y/m/d H:i:s");
?>
Output:
输出:
2013/03/15 00:00:00
回答by Dibyendu Mitra Roy
There are 3 ways that you can do:
您可以通过 3 种方式进行操作:
1) Using Laravel Model
1) 使用 Laravel 模型
$user = \App\User::find(1);
$newDateFormat = $user->created_at->format('d/m/Y');
dd($newDateFormat);
2) Using PHP strtotime
2)使用PHP strtotime
$user = \App\User::find(1);
$newDateFormat2 = date('d/m/Y', strtotime($user->created_at));
dd($newDateFormat2);
3) Using Carbon
3) 使用碳
$user = \App\User::find(1);
$newDateFormat3 = \Carbon\Carbon::parse($user->created_at)->format('d/m/Y');
dd($newDateFormat3);
回答by JustCarty
I had a similar problem, I wanted to change the format, but I also wanted the flexibility of being able to change the format in the blade template engine too.
我遇到了类似的问题,我想更改格式,但我也希望能够灵活地更改刀片模板引擎中的格式。
I, therefore, set my model up as the following:
因此,我将我的模型设置如下:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
\Carbon\Carbon::setToStringFormat('d-m-Y');
class User extends Model
{
protected $dates = [
'from_date',
];
}
The setToStringFormat
will set all the dates to use this format for this model.
The advantage of this for me is that I could have the format that I wanted without the mutator, because with the mutator, the attribute is returned as a string meaning that in the blade template I would have to write something like this if I wanted to change the format in the template:
该setToStringFormat
会设置所有日期使用这种格式的这种模式。
这对我来说的好处是,我可以在没有 mutator 的情况下拥有我想要的格式,因为使用 mutator,属性作为字符串返回,这意味着在刀片模板中,如果我想,我将不得不写这样的东西更改模板中的格式:
{{ date('Y', strtotime($user->from_date)) }}
Which isn't very clean.
这不是很干净。
Instead, the attribute is still returned as a Carbon instance, however it is first returned in the desired format.
That means that in the template I could write the following, cleaner, code:
相反,该属性仍作为 Carbon 实例返回,但它首先以所需格式返回。
这意味着我可以在模板中编写以下更简洁的代码:
{{ $user->from_date->format('Y') }}
In addition to being able to reformat the Carbon instance, I can also call various Carbon methods on the attribute in the template.
除了能够重新格式化 Carbon 实例之外,我还可以对模板中的属性调用各种 Carbon 方法。
There is probably an oversight to this approach; I'm going to wager it is not a good idea to specify the string format at the top of the model in case it affects other scripts. From what I have seen so far, that has not happened. It has only changed the default Carbon for that model only.
这种方法可能存在疏忽;我敢打赌,在模型顶部指定字符串格式不是一个好主意,以防它影响其他脚本。从我目前看到的情况来看,这并没有发生。它仅更改了该模型的默认碳。
In this instance, it might be a good set the Carbon format back to what it was originally at the bottom of the model script. This is a bodged idea, but it would work for each model to have its own format.
Contrary, if you are having the same format for each model then in your AppServiceProvider instead. That would just keep the code neater and easier to maintain.
在这种情况下,最好将 Carbon 格式设置回模型脚本底部的原始格式。这是一个笨拙的想法,但它适用于每个模型都有自己的格式。
相反,如果您为每个模型使用相同的格式,则在您的 AppServiceProvider 中。这只会使代码更整洁,更易于维护。
回答by kosta
You can use Carbon::createFromTimestamp
您可以使用 Carbon::createFromTimestamp
BLADE
刀刃
{{ \Carbon\Carbon::createFromTimestamp($user->from_date)->format('d-m-Y')}}
回答by Honest Knight
Sometimes changing the date format doesn't work properly, especially in Laravel. So in that case, it's better to use:
有时更改日期格式不能正常工作,尤其是在 Laravel 中。所以在这种情况下,最好使用:
$date1 = strtr($_REQUEST['date'], '/', '-');
echo date('Y-m-d', strtotime($date1));
Then you can avoid error like "1970-01-01"!
那么你就可以避免像“1970-01-01”这样的错误!