我正在尝试转换一些日期
<p>{{ Carbon\Carbon::parse($post->created_at)->diffForHumans() }}</p>
这工作正常,因为日期格式为 2017-05-01 10:52:51
但是,我也将日期存储为 unix 时间戳。
我怎样才能将这些转换为约会对象?
我已经试过了
{{ Carbon\Carbon::createFromTimestamp($post->created_at)->toDateTimeString() }}
错误 - 遇到格式不正确的数值
转储是
<pre class="xdebug-var-dump" dir="ltr">
<small>int</small>
<font color="#4e9a06">1493637826</font>
</pre>
最佳答案
您第一次尝试使用 createFromTimestamp()
是正确的,所以你的输入肯定有问题。这是一个工作示例:
>>> \Carbon\Carbon::createFromTimestamp(1493637826)->toDateTimeString();
=> "2017-05-01 11:23:46"
您遇到的错误表明您传入的是 DateTime 字符串,而不是 unix 时间戳:
>>> \Carbon\Carbon::createFromTimestamp('2017-05-01 11:23:46')->toDateTimeString();
PHP Notice: A non well formed numeric value encountered
关于php - 碳从时间戳创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43718154/