2012-07-08 102 views
1

我有最難的時間試圖將客戶端時間轉換爲服務器時間。原因是我每小時執行一次cron作業。我設法在java腳本中獲得時區偏移量,但我對如何應用時區偏移量一無所知。例如,我的時區偏移量是7。那又如何?我應該如何應用此抵消?JavaScript客戶端端時間到PHP服務器

,以獲得偏移我用

var offset = new Date().getTimezoneOffset(); 

服務器端是由PHP處理。

+0

http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with -this-javascript/6016329 – dm03514 2012-07-08 18:12:14

+0

爲什麼不在協議雙方使用[協調世界時](http://en.wikipedia.org/wiki/UTC)? – Andreas 2012-07-08 18:14:38

+1

我建議使用UNIX時間戳。 – Jashwant 2012-07-08 18:21:48

回答

1

正如其他人在評論中所說的,最好的方法是使用UNIX時間戳。在JavaScript中得到這個,使用下面的代碼:

var date = Math.round(new Date().getTime()/1000); 

getTime返回以毫秒爲單位的值,但我們希望以秒爲單位的值,所以我們通過1000

劃分它,那麼你可以使用AJAX將該值發送到服務器或將該值放在隱藏的表單字段中,並在用戶提交表單時將其發送到服務器。

在PHP中,你可以得到這樣的日期:

$date = new DateTime(); 
// 1341773609 is the UNIX timestamp, which I got from running the above 
// JavaScript and alerting the date 
$date->setTimestamp(1341773609); 
echo $date->format('Y-m-d H:i:s');