-1

我在 mysql 中有一个表,它有一个开始日期和结束日期。

1 行的示例是:

startdate -> 20121224
endate -> 20121226
title -> name

这将返回(输出!!!)

[{"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 为每个日期输出一个(唯一的区别是日期,请参见下文)我想要这个

[{"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));
4

2 回答 2

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()可能会帮助您进行日期操作。

于 2013-03-05T17:54:39.167 回答
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));
于 2013-03-05T17:56:17.060 回答