Convert Unix Timestamp to Carbon Object

左心房为你撑大大i 提交于 2019-12-20 10:16:41

问题


I have unix timestamp in table, wants to show to user using Carbon. How can I achieve ?

e.g.

1487663764.99256
To
2017-02-24 23:23:14.654621

回答1:


Did you check the carbon docs? Something like this? I think this is what youre looking for:

Carbon::createFromTimestamp(-1)->toDateTimeString(); 

Checkout http://carbon.nesbot.com/docs/#api-instantiation




回答2:


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:

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.

So you can just do:

$carbon = Carbon::createFromTimestamp($dbResult['SomeTimestampColumn']);


来源:https://stackoverflow.com/questions/42510921/convert-unix-timestamp-to-carbon-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!