confronto date curl
Sat May 17 2025 06:51:27 GMT+0000 (Coordinated Universal Time)
Saved by
@StefanoGi
<?php
// Esempio di chiamata cURL che ritorna un JSON con un timestamp
$curl = curl_init("https://esempio.com/api/data"); // Cambia con la tua URL
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
// Decodifica JSON (supponiamo che abbia un campo 'timestamp')
$data = json_decode($response, true);
$remoteTimestamp = isset($data['timestamp']) ? (int)$data['timestamp'] : null;
if ($remoteTimestamp === null) {
die("Timestamp non trovato nella risposta");
}
// Crea oggetto DateTime dal timestamp ricevuto
$remoteDate = (new DateTime())->setTimestamp($remoteTimestamp);
// Crea oggetto DateTime per 1 anno fa a mezzanotte
$localDate = new DateTime();
$localDate->modify('-1 year')->setTime(0, 0, 0);
// Confronto
if ($remoteDate < $localDate) {
echo "Il timestamp remoto è più vecchio di 1 anno.\n";
} else {
echo "Il timestamp remoto è più recente di 1 anno.\n";
}
// Debug: stampa date
echo "Data remota: " . $remoteDate->format('Y-m-d H:i:s') . "\n";
echo "Data di riferimento: " . $localDate->format('Y-m-d H:i:s') . "\n";
?>
content_copyCOPY
Comments