PHP:带有日期间隙的报告表

发布于 2024-10-08 07:36:31 字数 260 浏览 0 评论 0原文

我在数据库中有一个表,其中包含几天的摘要。有些日子可能没有这些值。 我需要显示包含结果的表格,其中每列都是用户选择范围内的一天。 我尝试使用时间戳(end_date - start_date / 86400 - 报告中有多少天,然后使用 DATEDIFF(row_date, 'user_entered_start_date') 并从该索引创建数组),但现在我已经有了一大堆解决方法夏季时间:( 有任何例子或想法如何使其正确吗?

PS 我需要在 PHP 端执行此操作,因为数据库负载很高。

I have a table in DB which contains summaries for days. Some days may not have the values.
I need to display table with results where each column is a day from user selected range.
I've tried to play with timestamp (end_date - start_date / 86400 - how many days in report, then use DATEDIFF(row_date, 'user_entered_start_date') and create array from this indexes), but now I've got whole bunch of workarounds for summer time :( Any examples or ideas how to make this correct?

P.S. I need to do this on PHP side, because DB is highly loaded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一口甜 2024-10-15 07:36:31

尝试 DateTime 对象:

$reportdate=date_create($user_startdate);
$interval=new DateInterval('P1D');//1 day interval

$query="SELECT s.start_date, s.end_date, s.info 
   FROM summary s 
   WHERE s.start_date>='$user_startdate' 
      AND s.end_date<='$user_enddate'";

$r=mysqli_query($db,$query);
while($row=$r->fetch_assoc()){
  $rowdate=create_date($row['start_date']);
  while($reportdate < $rowdate) {//won't work if $rowdate is a timestamp!  
    //output $reportdate and blank row
    $reportdate->add($interval); //increment date
  }
  //output $rowdate and $row[info]
  $reportdate->add($interval); //increment date

}

ETA 另一个选项:

根据您的评论,动态生成缺失的日期可能更容易。为此,您需要一个整数表、应出现在报告输出中的日期数、开始日期和日期增量。

在您的数据库中创建一个名为 numbers 的表并插入数字 0 到 9:

CREATE TABLE  numbers (
  num int(10) unsigned NOT NULL,
  PRIMARY KEY (num)
);

数字表可用于生成整数序列。例如,要获取从 1 到 20 的序列:

SELECT i FROM (
  SELECT 10*n1.num + n2.num AS i 
  FROM numbers n1 CROSS JOIN numbers n2) nums
WHERE i BETWEEN 1 AND 20
ORDER BY i ASC;

如果将像上面这样的序列查询左连接到常规查询,则应该能够生成真实行和空白行。例如

SELECT alldates.d, mystuff.* FROM
    (SELECT date_add($start_date, interval i day) AS d FROM 
        (SELECT 10*n1.num + n2.num AS i
         FROM numbers n1 CROSS JOIN numbers n2
         ORDER BY i ASC) nums
     WHERE i <= DATEDIFF($end_date,$start_date)) alldates
LEFT JOIN mystuff
ON alldates.d = mystuff.somedate
ORDER BY $whatever;

Try the DateTime object:

$reportdate=date_create($user_startdate);
$interval=new DateInterval('P1D');//1 day interval

$query="SELECT s.start_date, s.end_date, s.info 
   FROM summary s 
   WHERE s.start_date>='$user_startdate' 
      AND s.end_date<='$user_enddate'";

$r=mysqli_query($db,$query);
while($row=$r->fetch_assoc()){
  $rowdate=create_date($row['start_date']);
  while($reportdate < $rowdate) {//won't work if $rowdate is a timestamp!  
    //output $reportdate and blank row
    $reportdate->add($interval); //increment date
  }
  //output $rowdate and $row[info]
  $reportdate->add($interval); //increment date

}

ETA another option:

Based on your comments, it may be easier to dynamically generate the missing dates. For this you'll need an integer table, the number of dates that should appear in your report output, a start date and a date increment.

In your db create a table called numbers and insert the numbers 0 through 9:

CREATE TABLE  numbers (
  num int(10) unsigned NOT NULL,
  PRIMARY KEY (num)
);

The numbers table can be used for making sequences of integers. For instance, to get a sequence from 1 to 20:

SELECT i FROM (
  SELECT 10*n1.num + n2.num AS i 
  FROM numbers n1 CROSS JOIN numbers n2) nums
WHERE i BETWEEN 1 AND 20
ORDER BY i ASC;

If you left join a sequence query like the above to your regular query, you should be able to generate both real and blank rows. e.g.

SELECT alldates.d, mystuff.* FROM
    (SELECT date_add($start_date, interval i day) AS d FROM 
        (SELECT 10*n1.num + n2.num AS i
         FROM numbers n1 CROSS JOIN numbers n2
         ORDER BY i ASC) nums
     WHERE i <= DATEDIFF($end_date,$start_date)) alldates
LEFT JOIN mystuff
ON alldates.d = mystuff.somedate
ORDER BY $whatever;
回忆凄美了谁 2024-10-15 07:36:31

您可以使用空白值“预加载”数据库。然后执行 UPDATE 查询而不是插入。您所有没有数据的日子都将被预先填充。您可以创建一个简单的脚本来创建一个月/一年/十年的(空白)数据,并根据需要经常运行它。这样您就不必担心没有数据的日子如何进入数据库 - 您可以从数据“清零”开始。

You could "pre-load" the database with blank values. Then do UPDATE queries instead of inserts. All your days with no data will be pre-populated. You can create a simple script that creates a month's/year's/decade's worth of (blank) data and run it as often as you need. Then you never have to worry about how the days with no data get into the database - you start with your data "zeroed" out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文