在适当的日期字段中回显时间戳

时间:2014-05-17 13:48:10

标签: php html mysql

在mysql中我插入了unix纪元时间,它需要像日历一样显示在日历中。 每个时间都应该在代表当天的行中回显。

我拥有的代码就是这个,它在一列中回显了31天,所有时间戳都在一行中。如何把它们放在正确的日/行中?谢谢。

<table>
<tr>
<td><strong>day</strong></td>
<td><strong>timestamp</strong></td>
</tr>

<?php
$sel = "select time from rv where id=$id";
$rez = mysql_query($sel) or die(mysql_error());
$n = mysql_num_rows($rez);
for($day=1; $day<32; $day++){
    echo "<tr>";
    echo "<td>$day.</td>";
    while ($re = mysql_fetch_array($rez)){
        $time_b = $re["time"];

        $time_a = new DateTime("@$time_b");
        echo "<td>";
        echo $time_a->format('h:i:s');
        echo "</td>";
    }
    echo "</tr>";
}
?>
</table>

更新: 这是我现在得到的。我需要以正确的日期/行显示每个日期 http://s3.postimg.org/nvmlf1mtv/test.jpg

1 个答案:

答案 0 :(得分:0)

假设您希望数据库中的日期从1-31开始排序,那么您的工作方式就错了。

首先要对数据库中的值进行排序,按天存储然后再显示它们。

警告:我已将您的代码用于此答案,但不要使用mysql,请使用mysqli

//make an array with all 31 days
$mydays=array_fill(1,31,array());     
//loop the database results and fill the array
while ($re = mysql_fetch_array($rez)){
    $day=date('d',$re["time"]);
    //store the value in the $mydays array
    $mydays[$day][]=$re["time"];
    }

//now loop the days and display them
for($i=1;$i<32;$i++){

   if(count($mydays[$i])<1)continue;  //the is no data in this day
   // else loop the data inside this day
   foreach($mydays[$i] as $thisday){
        echo "<tr>";
        echo "<td>$i</td>";
        echo "<td>";
        echo date('h:i:s',$thisday);
        echo "</td>";
        echo "</tr>"
        }
   }