我在mysql中有一个表,它有一个startdate和enddate。
1行的示例是:
startdate -> 20121224
endate -> 20121226
title -> name
这将返回(OUTPUT !!!):
[{"user":"378","date":"UNIX_TIMESTAMP(startdate)","title":"name","description":"6 Days","url":"UNIX_TIMESTAMP(enddate)","bmanager":"manager name","academic_year":"20120801","division":"division name","manager_id":"3"}]
由于此记录超过3天,我希望JSON为每个日期输出一个(唯一的差异是日期,请参见下文)我想要这个INSTEAD
[{"user":"378","date":"UNIX_TIMESTAMP(1st date)","title":"name","description":"6 Days","url":"UNIX_TIMESTAMP(1st date)","bmanager":"manager name","academic_year":"20120801","division":"division name","manager_id":"3"},
{"user":"378","date":"UNIX_TIMESTAMP(2nd date)","title":"name","description":"6 Days","url":"UNIX_TIMESTAMP(2nd date)","bmanager":"manager name","academic_year":"20120801","division":"division name","manager_id":"3"},
{"user":"378","date":"UNIX_TIMESTAMP(3rd date)","title":"name","description":"6 Days","url":"UNIX_TIMESTAMP(3rd date)","bmanager":"manager name","academic_year":"20120801","division":"division name","manager_id":"3"},]
这是使用的脚本:
$sqldata = mysql_query('
SELECT
datediff(requests.end_date, requests.start_date) as numdays,
requests.user,
UNIX_TIMESTAMP(requests.start_date) AS date,
requests.employee AS title,
requests.days AS description,
UNIX_TIMESTAMP(requests.end_date) AS url,
business.line_manager AS bmanager,
requests.academic_year,
business.academic_year,
business.division,
line_managers.userid AS manager_id
FROM requests
INNER JOIN holiday_entitlement_business_manual AS business ON requests.user=business.userid AND requests.academic_year=business.academic_year
INNER JOIN line_managers ON business.line_manager=line_managers.name
WHERE requests.approved = 1
');
$posts = array();
while($row = mysql_fetch_assoc($sqldata))
{
$row['date'] = $row['date'].'000';
$row['url'] = $row['url'].'000';
$posts[] = $row;
}
mysql_free_result($sqldata);
die(json_encode($posts));
答案 0 :(得分:2)
尝试通过以下方式替换您的代码:
$sqldata = mysql_query('
SELECT
requests.start_date as startdate,
datediff(requests.end_date, requests.start_date) as numdays,
requests.user,
UNIX_TIMESTAMP(requests.start_date) AS date,
requests.employee AS title,
requests.days AS description,
UNIX_TIMESTAMP(requests.end_date) AS url,
business.line_manager AS bmanager,
requests.academic_year,
business.academic_year,
business.division,
line_managers.userid AS manager_id
FROM requests
INNER JOIN holiday_entitlement_business_manual AS business ON requests.user=business.userid AND requests.academic_year=business.academic_year
INNER JOIN line_managers ON business.line_manager=line_managers.name
WHERE requests.approved = 1
');
$posts = array();
while($row = mysql_fetch_assoc($sqldata))
{
$startDate = $row['startdate'];
unset($array['startdate']);
$dayDiff = $row['numdays'];
$row['date'] = $row['date'].'000';
$row['url'] = $row['url'].'000';
for($i = 0; $i <= $dayDiff; $i++)
{
$row['date'] = ...; // Do here the date parsing and add $i to the days
$posts[] = $row;
}
}
mysql_free_result($sqldata);
die(json_encode($posts));
这些函数dateparse()和mktime()可能会帮助您处理日期。
答案 1 :(得分:0)
不确定我是否明白你的观点。这可以给你一个起点。 基本上,您需要获取所有行,然后为您希望考虑的每一天创建一条记录。
$sqldata = mysql_query('
SELECT
datediff(requests.end_date, requests.start_date) as numdays,
requests.user,
UNIX_TIMESTAMP(requests.start_date) AS date,
requests.employee AS title,
requests.days AS description,
UNIX_TIMESTAMP(requests.end_date) AS url,
business.line_manager AS bmanager,
requests.academic_year,
business.academic_year,
business.division,
line_managers.userid AS manager_id
FROM requests
INNER JOIN holiday_entitlement_business_manual AS business ON requests.user=business.userid AND requests.academic_year=business.academic_year
INNER JOIN line_managers ON business.line_manager=line_managers.name
WHERE requests.approved = 1
');
$posts = array();
while($row = mysql_fetch_assoc($sqldata))
{
$row['date'] = $row['date'];
$row['url'] = $row['url'];
for($x = $row['date']; $x < $row['url']; $x+= 86400) {
// where 86400 makes you advancing by a day in secs
$row['date'] = $x;
$row['url'] = $x;
$posts[] = $row;
}
$posts[] = $row;
}
mysql_free_result($sqldata);
die(json_encode($posts));