PHP日期()为JavaScript新的Data() - 阵内 [英] PHP date() to JavaScript new Data() - Inside array

查看:93
本文介绍了PHP日期()为JavaScript新的Data() - 阵内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:这是一个后续问题<一href=\"http://stackoverflow.com/questions/28946553/google-charts-timeline-php-array-as-input/28951167?noredirect=1#comment46222045_28951167\">an先前的职位矿的。

Note: This is a follow-up question to an earlier post of mine.

我有一个PHP数组,我需要解析到JavaScript。目前,我试图这样做,使用&LT;?PHP的回声json_en code($ ARRAY_NAME)GT; JavaScript的内部。对于大部分的数组中的数据的,这工作正常,除了日期。我试图解析数组是这样的:

I have a PHP array which I need to parse to a JavaScript. Currently I am attempting to do so, using <?php echo json_encode($array_name)?> inside the JavaScript. For most of the data in the array, this works fine, except for the dates. The array I am trying to parse looks like this:

$timeData = array(
  array('1', 'Some task',        date('2015-04-09'), date('2015-04-23')),
  array('2', 'Another task',     date('2015-04-13'), date('2015-04-20')),
  array('3', 'A different task', date('2015-04-16'), date('2015-04-30))
);
?>

我需要解析这个数组,在某种程度上,使得它在可用谷歌图表时间轴,函数看起来像这样(见code评论)

I need to parse this array, in a way that makes it usable in Google Charts Timeline, where the function looks like this (see comment in code):

<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization',
       'version':'1','packages':['timeline']}]}"></script>
<script type="text/javascript">

google.setOnLoadCallback(drawChart);
function drawChart() {
  var container = document.getElementById('example2.1');
  var chart = new google.visualization.Timeline(container);
  var dataTable = new google.visualization.DataTable();

  dataTable.addColumn({ type: 'string', id: 'Term' });
  dataTable.addColumn({ type: 'string', id: 'Name' });
  dataTable.addColumn({ type: 'date', id: 'Start' });
  dataTable.addColumn({ type: 'date', id: 'End' });

  // This is the part I would like to replace with the PHP array
  dataTable.addRows([
    [ '1', 'George Washington', new Date(1789, 3, 29), new Date(1797, 2, 3) ],
    [ '2', 'John Adams',        new Date(1797, 2, 3),  new Date(1801, 2, 3) ],
    [ '3', 'Thomas Jefferson',  new Date(1801, 2, 3),  new Date(1809, 2, 3) ]]);

  chart.draw(dataTable);
}
</script>

我已经试过了PHP阵列中创建的日期,其中包括几种不同的方法新的Date()新的DateTime(),都没有运气。我最后的解决办法是将数组分割成块,解析数据之前。然而,这似乎有点傻,因为它是被放在一起作为一个数组再次JavaScript的内部。有什么办法来写PHP数组中的日期,这样,当解析到JavaScript的他们的工作?

I have tried several different ways of creating the dates in the PHP array, including new Date() and new DateTime(), all without luck. My final resort is to split the array up into pieces, before parsing the data. However this seems somewhat silly, as it is to be put together as an array again inside the JavaScript. Is there any way to write the dates in the PHP array, so that they work when parsed into the JavaScript?

推荐答案

在PHP端可以返回时间戳所以很容易在JavaScript处理:

In the PHP side you can return the timestamp so it is easier to handle in JavaScript:

<?php    
$timeData = array(
    array('1', 'Some task',        DateTime::createFromFormat('Y-m-d', date('2015-04-09'))->format('U')*1000, DateTime::createFromFormat('Y-m-d', date('2015-04-23'))->format('U')*1000,
    array('2', 'Another task',     DateTime::createFromFormat('Y-m-d', date('2015-04-09'))->format('U')*1000, DateTime::createFromFormat('Y-m-d', date('2015-04-20'))->format('U')*1000,
    array('3', 'A different task', DateTime::createFromFormat('Y-m-d', date('2015-04-16'))->format('U')*1000, DateTime::createFromFormat('Y-m-d', date('2015-04-30'))->format('U')*1000
);
?>

有关JavaScript的一部分,你可以再步行结果追加行:

For the JavaScript part you can then walk the result to append the rows:

/* Assuming rows contains a valid JS array with dates expressed as timestamps in milliseconds
    rows = [
        [1, "Some task", 1428570428000, 1429780165000],
        [2, "Another task", 1428570428000, 1429520977000],
        [3, "A different task", 1429175344000, 1430384993000]
    ];
*/
for (var i = 0; i < rows.length; i++) {
    dataTable.addRow(
        rows[i][0],
        rows[i][1],
        new Date(rows[i][2]),
        new Date(rows[i][3]),
    );
}

也可以根据谷歌的文档的日期和时间使用日期字符串重新presentation 。所以,你也可以从PHP返回此(注意,JS的几个月都​​是零索引):

It is also possible to send the date as string according Google's documentation Dates and Times Using the Date String Representation. So you can also return this from PHP (note that in JS the months are zero indexed):

<?php    
$timeData = array(
    array('1', 'Some task',        "Date(2015, 3, 9, 0, 0, 0, 0)", "Date(2015, 3, 23, 0, 0, 0, 0)",
    array('2', 'Another task',     "Date(2015, 3, 9, 0, 0, 0, 0)", "Date(2015, 3, 20, 0, 0, 0, 0)",
    array('3', 'A different task', "Date(2015, 3, 16, 0, 0, 0, 0)", "Date(2015, 3, 30, 0, 0, 0, 0)"
);
?>

和在JS只是做到这一点:

And in JS simply do this:

/* Assuming rows contains a valid JS array with dates expressed as strings
    rows = [
        [1, "Some task", "Date(2015, 3, 9, 0, 0, 0, 0)", "Date(2015, 3, 23, 0, 0, 0, 0)"],
        [2, "Another task", "Date(2015, 3, 9, 0, 0, 0, 0)", "Date(2015, 3, 20, 0, 0, 0, 0)"],
        [3, "A different task", "Date(2015, 3, 16, 0, 0, 0, 0)", "Date(2015, 3, 30, 0, 0, 0, 0)"]
    ];
*/
dataTable.addRows(rows);

这篇关于PHP日期()为JavaScript新的Data() - 阵内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆