【问题标题】:Convert PHP time to Javascript time in PHP在 PHP 中将 PHP 时间转换为 Javascript 时间
【发布时间】:2012-11-10 04:25:09
【问题描述】:

如何在 PHP 中将 PHP 时间转换为 Javascript 时间?我的 PHP 代码:

<?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>

【问题讨论】:

  • 如果我认为正确的话。也许js时间=php时间*1000?

标签: php javascript time


【解决方案1】:

使用 PHP 的 date() 的输出和 Javascript 时间的格式相同,但乘以 1000,因为它具有毫秒分辨率。

【讨论】:

【解决方案2】:

最终代码:

 <?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 函数”对于这种情况。我必须学习更多...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 2015-09-08
    • 2016-04-03
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多