Java & Php 的 Timestamp 為不同單位

一般以來,我以為 timestamp 應該是要從 1970 年零點零分零秒開始,至目前時刻所經過的「秒」數,才算是 timestamp 。不過後來查了一下 java 的 timestamp 的說明,他是從 1970 年零點零分零秒開始到現在的「微秒」數。所以二者之間相差了 1000 倍。 Java 的 timestamp的說明如下: long getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object. PHP 的 timestamp 說明如下: int time ( void ) Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). 所以如果 php & java 要作 web service 的服務的話,記得要處理這部份的轉換。 Java 部份的程式碼: [code lang=“java”] import java.util.Calendar; class Time { public static void main(String arg[]) { Calendar cal = Calendar.getInstance(); System.out.println(cal.getTime().getTime()); } } [/code] 結果如下: [code] whatup@whatup:/tmp$ java Time 1257688227061 [/code] PHP 部份的程式碼: [code] echo time(); [/code] 結果如下: [code] whatup@whatup:/tmp$ php time.php 1257688308 [/code]