// get date this week
$W = date('W',strtotime(date('Y-m-d')));
$sunday = date(datetime::ISO8601, strtotime(date('Y') . "W" . $W . "0"));
$this_week = date('Y-m-d',strtotime($sunday));
echo $this_week;
//looping for get all date this week
for ($i=1; $i <= 6; $i++) {
$date=date_create("$this_week");
date_add($date,date_interval_create_from_date_string("$i days"));
$date_week[] = date_format($date,"Y-m-d"); //value array
$event[] = "dinner"; // value array
$start[] = "start"; // key array
$title[] = "title"; // key array
}
我想将它改成像这样的json
{
title: 'dinner',
start: '2015-08-24'
},
{
title: 'dinner',
start: '2015-08-25'
},
{
title: 'dinner',
start: '2015-08-26'
},
{
title: 'dinner',
start: '2015-08-27'
},
{
title: 'dinner',
start: '2015-08-28'
},
{
title: 'dinner',
start: '2015-08-29'
}
答案 0 :(得分:0)
我认为这更好...... = 这仍然在循环中。
$output[] = [
'tile' => 'Dinner',
'start' => $date_week
];
这仍然在循环中。 然后在json_encode。
答案 1 :(得分:0)
您不需要组合所有这些数组。您可以在一个简单的循环中构建所需的数据结构
$W = date('W',strtotime(date('Y-m-d')));
$sunday = date(datetime::ISO8601, strtotime(date('Y') . "W" . $W . "0"));
$this_week = date('Y-m-d',strtotime($sunday));
$result = array();
for ($i=1; $i <= 6; $i++) {
$date=date_create($this_week);
date_add($date,date_interval_create_from_date_string("$i days"));
$result[] = array('title' => 'dinner', 'start' => date_format($date,"Y-m-d"));
}
echo json_encode($result);
结果是
[
{"title":"dinner","start":"2016-11-21"},
{"title":"dinner","start":"2016-11-22"},
{"title":"dinner","start":"2016-11-23"},
{"title":"dinner","start":"2016-11-24"},
{"title":"dinner","start":"2016-12-25"},
{"title":"dinner","start":"2016-12-26"}
]