为什么从我的时间戳算起的一年还有几个世纪?PHP/Wordpress



我在谷歌上搜索了很多,但一无所获,所以我希望你们能帮忙。

我称之为API(https://api.metals.live/v1/spot)。我正在使用PHP的date((函数转换时间戳,但我得到的日期在未来超过50000年。我将相同的时间戳复制并粘贴到这个epoch转换器中(https://www.epochconverter.com)而且效果很好。我还尝试过使用WP内置的WP_date((函数,在未来5万年。

有人有这个问题吗?

$url = "https://api.metals.live/v1/spot";
$response = file_get_contents('https://api.metals.live/v1/spot');
//convert to PHP array
$arr = json_decode($response,true);
// Loop thru data and add it to new array
$new_arr = [];
foreach($arr as $x => $x_value) {
foreach($x_value as $y => $y_value) {
echo "Key=" . $y . ", Value=" . $y_value;
echo "<br>";
array_push($new_arr, (object)[
'metal' => $y,
'price' => $y_value
]);
}
}
// print and format data in the array (for testing)
foreach($new_arr as $x) {
echo "<p>$x->metal : $x->price</p>" . "<br />";
}
// access gold price and convert to float
$gold_price = floatval($new_arr[0]->price);
// access silver price and convert to float
$silver_price = floatval($new_arr[1]->price);
//this is the timestamp value (despite it's name)
$timestamp = $new_arr[4]->price;
$date = date("Y-m-d H:i:s",$timestamp);
echo $date;

时间戳1638476352474太长,因为它包含毫秒。把它除以1000,你就会得到一个合适的日期。

$date = date("Y-m-d H:i:s",$timestamp/1000);

最新更新