2017-10-17 153 views
-1

我收到一個UNIX時間戳從URL像這樣:PHP碳正確格式化UNIX時間戳

/api/v1/order_variations/60?d=1508364000000 

當我取回並嘗試將時間戳轉換爲可讀的格式,碳輸出不正確的日期。

$timestamp = (int)$request->input('d'); 
$date = Carbon::createFromTimestamp($timestamp)->format('j F, Y'); 
dd($date); 

$時間戳值是15083.64億

碳轉化這「2月25日,49768」,但它應該是「2017年10月19日」

如果我使用:

Carbon::createFromTimeStampUTC($timestamp)->toDateTimeString(); 

我得到相同的結果。

任何想法我可能做錯了什麼?

+0

這個問題是針對PHP碳庫而不是將Java時間戳轉換爲PHP。請詳細說明您將此線程標記爲重複 –

回答

1

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?)。