gpt4 book ai didi

javascript - PHP date() 到 JavaScript new Data() - 数组内部

转载 作者:行者123 更新时间:2023-12-03 10:35:32 26 4
gpt4 key购买 nike

注意:这是 an earlier post of mine 的后续问题.

我有一个 PHP 数组,需要将其解析为 JavaScript。目前我正在尝试这样做,使用 <?php echo json_encode($array_name)?> JavaScript 内部。对于数组中的大多数数据,除了日期之外,这种方法都可以正常工作。我试图解析的数组如下所示:

$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))
);
?>

我需要解析这个数组,使其可以在 Google Charts Timeline 中使用。 ,其中函数如下所示(请参阅代码中的注释):

<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 数组中创建日期,包括 new Date()new DateTime() ,一切都没有运气。我的最后手段是在解析数据之前将数组分成几部分。然而,这似乎有些愚蠢,因为它要在 JavaScript 中再次组合为数组。有什么方法可以将日期写入 PHP 数组中,以便它们在解析为 JavaScript 时起作用吗?

最佳答案

在 PHP 端,您可以返回时间戳,这样在 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 部分,您可以遍历结果以追加行:

/* 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]),
);
}

也可以根据 Google 文档 Dates and Times Using the Date String Representation 将日期作为字符串发送。因此,您也可以从 PHP 返回此值(请注意,在 JS 中,月份索引为零):

<?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 中只需这样做:

/* 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);

关于javascript - PHP date() 到 JavaScript new Data() - 数组内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29004501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com