将 Unix 时间戳转换为 Carbon 对象
Convert Unix Timestamp to Carbon Object
我在 table 中有 unix 时间戳,想用 Carbon 显示给用户。我怎样才能实现?
例如
1487663764.99256
至
2017-02-24 23:23:14.654621
the Carbon documentation 中描述了多种创建 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.
所以你可以这样做:
$carbon = Carbon::createFromTimestamp($dbResult['SomeTimestampColumn']);
你检查碳文件了吗?我想这就是您要找的:
Carbon::createFromTimestamp(-1)->toDateTimeString();
如果您真的喜欢流畅的方法调用并且对使用构造函数时必要的额外行或一对难看的括号感到沮丧,您会喜欢解析方法:
Carbon::parse(1487663764);
Carbon::parse('first day of next month');
我在 table 中有 unix 时间戳,想用 Carbon 显示给用户。我怎样才能实现?
例如
1487663764.99256至
2017-02-24 23:23:14.654621
the Carbon documentation 中描述了多种创建 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.
所以你可以这样做:
$carbon = Carbon::createFromTimestamp($dbResult['SomeTimestampColumn']);
你检查碳文件了吗?我想这就是您要找的:
Carbon::createFromTimestamp(-1)->toDateTimeString();
如果您真的喜欢流畅的方法调用并且对使用构造函数时必要的额外行或一对难看的括号感到沮丧,您会喜欢解析方法:
Carbon::parse(1487663764);
Carbon::parse('first day of next month');