2012-11-10 40 views
0

如何將PHP時間轉換爲PHP中的Javascript時間?我的PHP代碼:將PHP時間轉換爲PHP中的Javascript時間

<?php 
$times = array(/* A list of time with "Y-m-d h:i:s" format goes here */); 
$return = '['; 
$i = 1; 
foreach($times as $time) { 
    $return .= '['.strtotime($time).','.$i.'],'; 
    //In my opinion, converting time to 
    //js time at here is good choice 
    if($i==100) { 
    break; 
    } 
    $i++; 
} 
$return = substr($return, 0, -1).']'; 
?> 

我的腳本:

<script> 
var results = [{ 
    label: "Buy in", 
    data: <?=$return?> 
}]; 
</script> 
+0

如果我認爲正確。也許js時間= php時間* 1000? –

回答

5

使用PHP的date()和Javascript時間輸出相同的格式,但是乘以1000,因爲它具有毫秒。

+1

做得好NullPoint,quanganh_developer請在嘗試使用谷歌之前問:) http://stackoverflow.com/questions/5403410/question-about-time-functions-in-php-and-javascript-particularly-the-gettime-fun –

+0

謝謝@NullPoint!只是我的錯誤:D!謝謝@Jared德雷克! –

0

最終代碼:

<?php 
     $times = array(/* A list of time with "Y-m-d h:i:s" format goes here */); 
     $return = '['; 
     $i = 1; 
     foreach($times as $time) 
     { 
      $return .= ('['.strtotime($time)*1000).','.$i.'],'; 
      if($i==100) 
      { 
       break; 
      } 
      $i++; 
     } 
     $return = substr($return, 0, -1).']'; 
?> 

非常感謝你:d!有時候我有愚蠢的想法:「如何從php代碼中調用js函數」。我必須學習越來越多......