使用 Laravel 4 和 Carbon 的 PHP 不会从我的数据库中打印出日期时间字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18561097/
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-14 08:22:07  来源:igfitidea点击:

PHP using Laravel 4 and Carbon will not print out a DateTime field from my Database

phplaravel

提问by JasonDavis

I am building a PHP application with Laravel 4.

我正在用 Laravel 4 构建一个 PHP 应用程序。

I am getting errors when I try to print out a DateTime record from the Database though.

但是,当我尝试从数据库中打印出 DateTime 记录时出现错误。

{{ $user->created_at }}

{{ $user->created_at }}

Gives me this error

给了我这个错误

InvalidArgumentException
Trailing data
open: E:\Server\htdocs\projects\timeclock\www\vendor\nesbot\carbon\src\Carbon\Carbon.php

InvalidArgumentException
尾随数据
打开:E:\Server\htdocs\projects\timeclock\www\vendor\nesbot\carbon\src\Carbon\Carbon.php

Very frustrating!

非常令人沮丧!

An example value from that Database field is: 2013-08-31 20:50:25.

来自该数据库字段的示例值是:2013-08-31 20:50:25

回答by Oli Folkerd

You are missing the milisecond data on the time stamp, you need to use:

您缺少时间戳上的毫秒数据,您需要使用:

Carbon::createFromFormat('Y-m-d H:i:s.u', $value)->format('d/m/Y H:i:s');

回答by Joseph Silber

You have to formatit:

你必须这样format做:

{{ $user->created_at->format('h:i:s') }}

The PHP docshas a list of all the codes available to use as a format.

PHP 文档列出了所有可用作格式的代码。

回答by Rueian Oneecom

I have the same issue.

我有同样的问题。

And I found that this is caused by my timestamp data in database.

我发现这是由我在数据库中的时间戳数据引起的。

2013-12-13 22:40:50.561709 <- this one will cause the issue.

2013-12-13 22:40:50.561709 <- 这会导致问题。

2013-12-13 22:40:50 <- this one will not.

2013-12-13 22:40:50 <- 这个不会。

回答by tosin

Timestamp value with millisecond causes this issue.

带毫秒的时间戳值会导致此问题。

Column which is converted to Carbon object can not have millisecond timestamp.(default: created_at, updated_at). http://readouble.com/laravel/4/2/0/en/eloquent.html#date-mutators

转换为 Carbon 对象的列不能有毫秒时间戳。(默认:created_at,updated_at)。 http://readouble.com/laravel/4/2/0/en/eloquent.html#date-mutators

If Carbon Object is not necessary, you can disallow auto-converting.

如果不需要 Carbon Object,您可以禁止自动转换。

class SomeModel extends Eloquent {

    public function getDates()
    {
        return array();
    }
}

But it also make Carbon methods(ex:->format()) unavailable. You have to format timestamps in other way.

但它也使 Carbon 方法(例如:->format())不可用。您必须以其他方式格式化时间戳。