timestamp มันไม่ถูกต้องหรือเปล่าครับ
1623152365215 แปลงเป็นวันที่ได้ 53405-08-27 00:00:15
18231526489465 แปลงได้ 579704-01-29 08:44:25
ลองเอาค่าจริงๆมาดูจะดีกว่าจะได้รู้ว่าของจริงๆมันเป็นยังไง
อันนี้ตัวอย่างโค้ดที่ผมเอาไว้ตรวจเวลาระหว่างเครื่องที่ใช้ (JS) กับ server (PHP).
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Expires: 0'); // Proxies.
$timestamp = time();
$phpdatetime = date('Y-m-d H:i:s', $timestamp);
$headers = [];
$requestHeaders = getallheaders();
if (is_array($requestHeaders)) {
foreach ($requestHeaders as $key => $value) {
$headers[strtolower(trim($key))] = trim($value);
}
unset($key, $value);
}
unset($requestHeaders);
if (
isset($headers['accept']) &&
(
stripos($headers['accept'], 'html') === false &&
stripos($headers['accept'], '*/*') === false
)
) {
header('Content-Type: application/json');
$output = [];
$output['datetime'] = $phpdatetime;
$output['timestamp'] = $timestamp;
echo json_encode($output);
unset($output);
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Date/time now</title>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
table {
border: 1px solid #ccc;
border-collapse: collapse;
}
table td,
table th {
border: 1px solid #ccc;
padding: 5px;
text-align: left;
}
.not-matched-datetime {
background-color: rgba(255, 0, 0, .3);
}
</style>
</head>
<body>
<h1>Date/time</h1>
<table>
<thead>
<tr>
<th>Server date/time</th>
<th>Your date/time</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="phpDateTime"><?php echo $phpdatetime; ?></span></td>
<td><span id="jsDateTime"></span></td>
</tr>
<tr>
<td><span id="phpTimestamp"><?php echo $timestamp; ?></span></td>
<td><span id="jsTimestamp"></span></td>
</tr>
</tbody>
</table>
<script>
function addZero(number) {
number = String(number);
return number.padStart(2, '0');
}
function displayJSDatetime() {
let Datetime = new Date();
let dateString = Datetime.getFullYear() '-' addZero(parseInt(Datetime.getMonth()) 1) '-' addZero(Datetime.getDate());
dateString = ' ';
dateString = addZero(Datetime.getHours()) ':' addZero(Datetime.getMinutes()) ':' addZero(Datetime.getSeconds());
document.querySelector('#jsDateTime').innerHTML = dateString;
document.querySelector('#jsTimestamp').innerHTML = Datetime.getTime();
}
document.addEventListener('DOMContentLoaded', (event) => {
// display date/time of this device on load.
displayJSDatetime();
// then set interval to update them.
let intervalId = setInterval(() => {
// display JS date/time on this device.
displayJSDatetime();
// ajax get server date/time.
fetch('<?php echo $_SERVER['REQUEST_URI']; ?>', {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then((response) => {
return response.json();
})
.then((response) => {
if (response.datetime) {
let phpDatetime = document.querySelector('#phpDateTime');
let jsDatetime = document.querySelector('#jsDateTime');
let phpTimestamp = document.querySelector('#phpTimestamp');
phpDatetime.innerHTML = response.datetime;
phpTimestamp.innerHTML = response.timestamp;
if (phpDatetime.innerHTML !== jsDatetime.innerHTML) {
jsDatetime.classList.add('not-matched-datetime');
} else {
jsDatetime.classList.remove('not-matched-datetime');
}
}
});
}, 1000);
});
</script>
</body>
</html>
จากโค้ดที่ผมใช้ JS, PHP จะได้หลักไม่เท่ากันแต่เวลาไม่ต่างกันครับ
ตัวอย่าง PHP 1629715239
JS 1629715239369