在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:46:17  来源:igfitidea点击:

Convert timestamp to date in Laravel?

laravellaravel-4eloquent

提问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_ator updated_atcolumn, it's custom attribute, call it birthdayif you wish.

它不是created_atupdated_at列,它是自定义属性,birthday如果您愿意,可以调用它。

采纳答案by The Alpha

In this case, you need to convert the date within the query, if mysqlis 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_atare 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_birthdayto 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 getDatesaccessor that is build into Laravel.

恕我直言,使用 Laravel 管理日期的更好方法是使用getDatesLaravel 中内置的访问器。

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 Carbonobject. 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')