PHP Carbon 错误地格式化了 UNIX 时间戳
PHP Carbon incorrectly formatting UNIX timestamp
我从 URL 收到一个 UNIX 时间戳,如下所示:
/api/v1/order_variations/60?d=1508364000000
当我检索并尝试将时间戳转换为可读格式时,Carbon 输出了错误的日期。
$timestamp = (int)$request->input('d');
$date = Carbon::createFromTimestamp($timestamp)->format('j F, Y');
dd($date);
$timestamp的值为1508364000000.
Carbon 将其转换为 "25 February, 49768" 但它应该是 "19 October, 2017"
如果我使用:
Carbon::createFromTimeStampUTC($timestamp)->toDateTimeString();
我得到了相同的结果。
知道我可能做错了什么吗?
Unix 时间戳是自纪元(1970 年 1 月 1 日)以来的 秒 数,但您使用的是 毫秒 .只需将该值除以 1000 即可得到秒数。
$timestamp = (int)$request->input('d');
$timestamp = intval($timestamp / 1000); // convert milliseconds to seconds
这会产生值“2017 年 10 月 18 日 22:00:00”。从中获取值“2017 年 10 月 19 日”的唯一方法是使用偏移量为 +02:00 的时区(CEST?SAST?Africa/Johannesburg?)。
我从 URL 收到一个 UNIX 时间戳,如下所示:
/api/v1/order_variations/60?d=1508364000000
当我检索并尝试将时间戳转换为可读格式时,Carbon 输出了错误的日期。
$timestamp = (int)$request->input('d');
$date = Carbon::createFromTimestamp($timestamp)->format('j F, Y');
dd($date);
$timestamp的值为1508364000000.
Carbon 将其转换为 "25 February, 49768" 但它应该是 "19 October, 2017"
如果我使用:
Carbon::createFromTimeStampUTC($timestamp)->toDateTimeString();
我得到了相同的结果。
知道我可能做错了什么吗?
Unix 时间戳是自纪元(1970 年 1 月 1 日)以来的 秒 数,但您使用的是 毫秒 .只需将该值除以 1000 即可得到秒数。
$timestamp = (int)$request->input('d');
$timestamp = intval($timestamp / 1000); // convert milliseconds to seconds
这会产生值“2017 年 10 月 18 日 22:00:00”。从中获取值“2017 年 10 月 19 日”的唯一方法是使用偏移量为 +02:00 的时区(CEST?SAST?Africa/Johannesburg?)。