laravel 将 Unix 时间戳转换为 Carbon 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42510921/
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 Unix Timestamp to Carbon Object
提问by Chintan7027
I have unix timestamp in table, wants to show to user using Carbon. How can I achieve ?
我在表中有 unix 时间戳,想显示给使用 Carbon 的用户。我怎样才能实现?
e.g.
例如
1487663764.99256到
2017-02-24 23:23:14.654621
回答by surgiie
Did you check the carbon docs? Something like this? I think this is what youre looking for:
你检查了碳文件吗?像这样的东西?我认为这就是你要找的:
Carbon::createFromTimestamp(-1)->toDateTimeString();
回答by IMSoP
There are several ways to create Carbon instances described in the Carbon documentation, which is linked at the bottom of the project's README. The relevant section is this:
Carbon 文档中描述了多种创建 Carbon 实例的方法,该文档位于项目自述文件的底部。相关部分是这样的:
The final two create functions are for working with unix timestamps. The first will create a Carbon instance equal to the given timestamp and will set the timezone as well or default it to the current timezone. The second, createFromTimestampUTC(), is different in that the timezone will remain UTC (GMT). The second acts the same as Carbon::createFromFormat('@'.$timestamp) but I have just made it a little more explicit. Negative timestamps are also allowed.
最后两个创建函数用于处理 unix 时间戳。第一个将创建一个等于给定时间戳的 Carbon 实例,并将设置时区或将其默认为当前时区。第二个 createFromTimestampUTC() 的不同之处在于时区将保持 UTC (GMT)。第二个行为与 Carbon::createFromFormat('@'.$timestamp) 相同,但我只是让它更明确一点。也允许使用负时间戳。
So you can just do:
所以你可以这样做:
$carbon = Carbon::createFromTimestamp($dbResult['SomeTimestampColumn']);