laravel 碳从时间戳创建

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

Carbon create from timestamp

phplaraveldatephp-carbon

提问by LeBlaireau

I am trying to convert some dates

我正在尝试转换一些日期

<p>{{  Carbon\Carbon::parse($post->created_at)->diffForHumans()  }}</p>

This works fine as the date is in the format 2017-05-01 10:52:51

这工作正常,因为日期的格式为2017-05-01 10:52:51

However I also have the dates stored as unix timestamps.

但是,我也将日期存储为 unix 时间戳。

How can I covert these to a date?

我怎样才能将这些转换为约会对象?

I already tried

我已经试过了

{{  Carbon\Carbon::createFromTimestamp($post->created_at)->toDateTimeString()  }}

Error - A non well formed numeric value encountered

错误 - 遇到格式不正确的数值

The dump is

转储是

 <pre class="xdebug-var-dump" dir="ltr">
<small>int</small> 
<font color="#4e9a06">1493637826</font>
    </pre>

回答by Matt K

Your first attempt using createFromTimestamp()is correct, so there must be something wrong with your input. Here is a working example:

您第一次尝试使用createFromTimestamp()是正确的,因此您的输入肯定有问题。这是一个工作示例:

>>> \Carbon\Carbon::createFromTimestamp(1493637826)->toDateTimeString();
=> "2017-05-01 11:23:46"

There error you encountered suggests you're passing in a DateTime string, not a unix timestamp:

您遇到的错误表明您传入的是 DateTime 字符串,而不是 unix 时间戳:

>>> \Carbon\Carbon::createFromTimestamp('2017-05-01 11:23:46')->toDateTimeString();
PHP Notice:  A non well formed numeric value encountered

回答by LeBlaireau

In the end I had to use gmdate to convert the timestamp.

最后我不得不使用 gmdate 来转换时间戳。

{{  Carbon\Carbon::parse(gmdate("Y-m-d\TH:i:s", $post->date_posted))->diffForHumans()  }}

It is strange because it should work the way Robbie Averill suggested.

这很奇怪,因为它应该按照 Robbie Averill 建议的方式工作。