I've googled this a bunch and got nothing so I'm hoping y'all can help.
I'm calling this 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) and it worked just fine. I've also tried using WP's built-in wp_date() function, still 50k years in the future.
Anyone had this issue?
$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;
CodePudding user response:
The timestamp 1638476352474
is too long because it contains milliseconds. Divide it by 1000, and you'll get a proper date.
$date = date("Y-m-d H:i:s",$timestamp/1000);