为什么我的时间戳中的年份差了几个世纪? PHP/Wordpress
Why is year from my timestamp centuries off? PHP/Wordpress
我在谷歌上搜索了很多,但一无所获,所以希望大家能提供帮助。
我称之为 API (https://api.metals.live/v1/spot). I'm converting the timestamp using PHP's date() function but I get back a date that is over 50,000 years in the future. I copied and pasted the same timestamp into this epoch converter (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);
我在谷歌上搜索了很多,但一无所获,所以希望大家能提供帮助。
我称之为 API (https://api.metals.live/v1/spot). I'm converting the timestamp using PHP's date() function but I get back a date that is over 50,000 years in the future. I copied and pasted the same timestamp into this epoch converter (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);