當我在我的php文件中使用getDate()
時,我總是得到比系統時間滯後5個小時的時間。所以我使用JavaScript從系統獲取時間。我的代碼是將數據從JavaScript傳遞到同一文件中的php
<script type="text/javascript">
Date.prototype.today = function() {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"."+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"."+ this.getFullYear();
}
Date.prototype.timeNow = function() {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
var newDate = new Date();
var datetime = newDate.today() + " " + newDate.timeNow();
document.write(datetime);
</script>
這是一個php文件。現在我想在我的php代碼中使用「datatime」
<?php
//Here I use datetime
?>
如何使用它?提前致謝。
JavaScript運行在客戶端,因此獲得客戶端時間。 PHP在服務器上運行並因此獲取服務器時間。這兩個時間在他們的情況下應該是正確的。您需要評估爲什麼您認爲需要將客戶端的時間存儲在服務器上,前提是客戶端可能會隨時更改時區。 – apokryfos
爲什麼不在PHP中設置時間並傳遞給JavaScript?現在你將不得不Ajax的時間到PHP爲我們未知的原因 – mplungjan
JavaScript是客戶端,而PHP運行在服務器上,爲什麼你不設置你的PHP代碼中的具體時區? [date_timezone_set](http://php.net/manual/en/datetime.settimezone.php) – hassan