在 Laravel 中将时间戳转换为日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23129367/
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
Convert timestamp to date in Laravel?
提问by Hyman
How can I return from the database all rows with a timestamp converted to a date (d-m-Y H:i)
, using the all()
method?
如何(d-m-Y H:i)
使用该all()
方法从数据库返回时间戳转换为日期的所有行?
It's not created_at
or updated_at
column, it's custom attribute, call it birthday
if you wish.
它不是created_at
或updated_at
列,它是自定义属性,birthday
如果您愿意,可以调用它。
采纳答案by The Alpha
In this case, you need to convert the date within the query
, if mysql
is being used as your database then you may use a raw query like this:
在这种情况下,您需要转换query
, 如果mysql
正在用作数据库中的日期,那么您可以使用这样的原始查询:
// Assumed my_birthday is like 1255033470
$raw = DB::raw("date_format(from_unixtime(my_birthday),'%b %d, %Y %l:%i %p') as dob");
$result = Model::get(array('id', 'username', $raw));
You may write it within single line:
您可以将其写在一行中:
$result = Model::get(array('id', 'username', DB::raw("...")));
回答by zwacky
timestamp columns like created_at
are being parsed first. the Uis simply the timestamp format. you can return your own format. for other formats see date docs.
时间戳列喜欢created_at
首先被解析。该ü简直是时间戳格式。您可以返回自己的格式。对于其他格式,请参阅日期文档。
edit: as stated in my comment, getDateFormat()
is for both ways (insert, selects). your best bet would be using format inside the model. example:
编辑:如我的评论中所述,getDateFormat()
适用于两种方式(插入、选择)。您最好的选择是在模型中使用格式。例子:
public function getMyBirthdayAttribute()
{
return $this->my_birthday->format('d.m.Y');
}
use $model->my_birthday
to call the attribute.
用于$model->my_birthday
调用属性。
// controller
$posts = Post::all();
// within sometemplate.blade.php
@foreach($posts as $post)
my formatted date: {{ $post->my_birthday }}
@endforeach
回答by T2theC
The better way to manage dates with Laravel IMHO is to use the getDates
accessor that is build into Laravel.
恕我直言,使用 Laravel 管理日期的更好方法是使用getDates
Laravel 中内置的访问器。
All you need to do is to set a method on your Model as such.
您需要做的就是在您的模型上设置一个方法。
public function getDates()
{
return [
'my_birthday',
'created_at',
'updated_at',
];
}
This will return a Carbon
object. Carbon is insanely awesome for manipulating dates.
这将返回一个Carbon
对象。碳在处理日期方面非常棒。
It is what Laravel does by default to created_at and updated_at.
这是 Laravel 默认对 created_at 和 updated_at 所做的。
You can then do things like:
然后,您可以执行以下操作:
$my_birthday->diffForHumans() // 2 Days ago
$my_birthday->format('d.m.Y') // 20.02.1979
There are tons of helpers here for you. Check out the Carbon docs: https://github.com/briannesbitt/Carbon
这里有很多帮手。查看碳文档:https: //github.com/briannesbitt/Carbon
Definitely worth learning. It might not be easy to understand for a beginner. However, I would advise you take a look and get your head around it. It will change your life - Dates can be a pain!
绝对值得学习。初学者可能不太容易理解。但是,我建议您看一看并了解它。它会改变你的生活 - 约会可能会很痛苦!
回答by Syafil
Just place this on your code $post it depend on what you create
只需将其放在您的代码 $post 上,这取决于您创建的内容
$post ->created_at->format('d.m.Y')