0

How to convert PHP time to Javascript time in PHP? My PHP code:

<?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).']';
?>

My script:

<script>
var results = [{
  label: "Buy in",
  data: <?=$return?>
}];
</script>
1
  • If I think correctly. Maybe js time = php time*1000? Commented Nov 10, 2012 at 4:30

2 Answers 2

5

use the output of PHP's date() and Javascript time is in the same format, but multiplied by 1000 because it has millisecond resolution.

Sign up to request clarification or add additional context in comments.

2 Comments

Well done NullPoint, quanganh_developer please try to use google before asking :) stackoverflow.com/questions/5403410/…
Thank you @NullPoint! Just my mistake:D! Thank @Jared Drake too!
0

Final code:

 <?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).']';
 ?>

Thank you very much:D! Some time I have stupid thinking like :"How to call js function from php code" for this case. I have to learn more and more...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.