Featured post
datetime - PHP: report table with date gaps -
i have table in db contains summaries days. days may not have values. need display table results each column day user selected range. i've tried play timestamp (end_date - start_date / 86400 - how many days in report, use datediff(row_date, 'user_entered_start_date') , create array indexes), i've got whole bunch of workarounds summer time :( examples or ideas how make correct?
p.s. need on php side, because db highly loaded.
try datetime object:
$reportdate=date_create($user_startdate); $interval=new dateinterval('p1d');//1 day interval $query="select s.start_date, s.end_date, s.info summary s s.start_date>='$user_startdate' , 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 timestamp! //output $reportdate , blank row $reportdate->add($interval); //increment date } //output $rowdate , $row[info] $reportdate->add($interval); //increment date }
eta option:
based on comments, may easier dynamically generate missing dates. you'll need integer table, number of dates should appear in report output, start date , date increment.
in db create table called numbers
, insert numbers 0 through 9:
create table numbers ( num int(10) unsigned not null, primary key (num) );
the numbers table can used making sequences of integers. instance, sequence 1 20:
select ( select 10*n1.num + n2.num numbers n1 cross join numbers n2) nums between 1 , 20 order asc;
if left join sequence query above regular query, should able generate both real , blank rows. e.g.
select alldates.d, mystuff.* (select date_add($start_date, interval day) d (select 10*n1.num + n2.num numbers n1 cross join numbers n2 order asc) nums <= datediff($end_date,$start_date)) alldates left join mystuff on alldates.d = mystuff.somedate order $whatever;
- Get link
- X
- Other Apps
Comments
Post a Comment