Reputation: 295
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>
Upvotes: 0
Views: 3480
Reputation: 295
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...
Upvotes: 0
Reputation: 57322
use the output of PHP's date()
and Javascript time is in the same format, but multiplied by 1000 because it has millisecond resolution.
Upvotes: 5