将DateTime从PHP返回到JS的良好实践 [英] Good practice to return DateTime from PHP to JS
问题描述
我创建了文件api.php
I've created file api.php
<?php
// Prevent caching.
header('Cache-Control: no-cache, must-revalidate');
// The JSON standard MIME header.
header('Content-type: application/json');
//mssql_select_db("**", mssql_connect(**));
$conn=mssql_connect(***);
$query = "SELECT ROUND([AirTemp_C],1) as [T]
,[DT]
FROM [ASUTP].[dbo].[Temperature_ER]
WHERE [Place] = 'ER06' AND [DT] > '26.05.2015 11:00'";
$qwr_res = mssql_query($query);
while ($row=mssql_fetch_array($qwr_res))
{
$temps[] = array (
'x' => $row['DT'],
'y' => $row['T']
);
}
echo json_encode($temps);
?>
它返回JSON,例如[{ x: 2015-05-26 11:02:04 , y:26.3}]和我的测量值。
It returns JSON like [{"x":"2015-05-26 11:02:04","y":26.3}] with my measurments.
我想使用此数据通过canvasjs-1.6.2绘制图形。这是我的代码:
window.onload = function(){
I want to use this data to draw graph using canvasjs-1.6.2. Here is my code: window.onload = function () {
$.getJSON("api.php",function(data1)
{
var chart = new CanvasJS.Chart("chartContainer",
{
title:{
text: "Title",
fontSize: 30
},
animationEnabled: true,
zoomEnabled:true,
height: 500,
axisX:{
gridColor: "Silver",
tickColor: "silver",
labelAngle: -80,
valueFormatString: "DD.MM HH:mm"
},
toolTip:{
shared:true
},
theme: "theme2",
axisY: {
gridColor: "Silver",
tickColor: "silver"
},
legend:{
verticalAlign: "center",
horizontalAlign: "right"
},
data: [
{
type: "line",
showInLegend: true,
lineThickness: 2,
name: "T",
//markerType: "square",
color: "#F08080",
dataPoints: data1
}
],
legend:{
cursor:"pointer",
itemclick:function(e){
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else{
e.dataSeries.visible = true;
}
chart.render();
}
}
});
chart.render();
});
但是,似乎这张图表(或根本没有JS)不能使用我的Date值作为正确的DateTime因此,图表无法呈现。
But it seems that this chart (or JS at all) cant use my Date value as a correct DateTime so chart can't render.
我已通过以下循环修复了此问题:
I've fixed this issue with such loop:
for(var i=0;i<data1.length;i++)
{
data1[i].x = new Date(data1[i].x);
}
我应该注意,我使用的是PHP 5.2,但我不允许升级它。
所以我的问题是:
I should notice that I'm using PHP 5.2 and I'm not allowed to upgrade it. So my questions are:
- 通过api.php获取图表数据的方法是否正确?
- 有没有办法在不进行循环转换的情况下将正确的DateTime值从php传递到JS?
推荐答案
如注释中所述,最可靠的跨浏览器传输日期的方式是使用时间值。从1970-01-01T00:00:00Z开始,UNIX时间值以秒为单位。 Javascript时间值来自同一纪元,但是使用毫秒,因此您只需将UNIX时间值乘以1000,然后直接传递给Date构造函数即可。
As noted in comments, the most reliable cross-browser way to transfer dates is using a time value. UNIX time values are in seconds since 1970-01-01T00:00:00Z. Javascript time values are from the same epoch, but use milliseconds so you just multiply UNIX time values by 1000 and pass directly to the Date constructor.
因此JSON可能类似于:
So the JSON might be like:
'[{x:"1432788147300", y:26.3}]'
,如果将 x 值读入名为 unixTimeValue 的变量,则代码为像这样:
and if the x value is read into a variable called unixTimeValue, the code would be like:
var javascriptDate = new Date(unixTimeValue * 1000);
但是,如果无法执行此操作,则可以解析'2015-05-26 11:02:04'相当容易。假设日期为世界标准时间,则可以使用 Date.UTC 将其转换为日期对象:
However, if you can't do that, you can parse strings like '2015-05-26 11:02:04' fairly easily. Assuming that the date is UTC, you can use Date.UTC to convert it to a date object:
function parseISOUTC(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0],b[1],b[2],b[3],b[4],b[5]));
}
这篇关于将DateTime从PHP返回到JS的良好实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!