-2

In mysql I have unix epoch time inserted and it needs to be displayed in calendar like table. Every timedate should be echoed in row that represents that day.

Code that I have is this and it echoes 31 days in one column and all timestamps in one row. How to put them in correct day/row? Thank you.

<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>

Update: Here is what I get now. And I need every date to be displayed in correct day/row http://s3.postimg.org/nvmlf1mtv/test.jpg

Jakob
  • 3,493
  • 4
  • 26
  • 42
  • How do you mean 'put them in correct day/row'? Do you want them sorted? from 1-31? – Michel May 17 '14 at 13:54
  • You should make the question more clear. i dont understand what the question is. – Sven van den Boogaart May 17 '14 at 13:56
  • And don't use mysql, use mysqli or pdo. Mysql is deprecated: [see here](http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future-use-mysqli) – Michel May 17 '14 at 13:57
  • i updated my question. @Michel - I know that mysql is deprecated but this is my problem right now so later I'll do mysqli or pdo – Jakob May 17 '14 at 14:03

1 Answers1

0

Supposing you want your days from the database sorted from 1-31, your working the wrong way rond.

You first want to sort the values from the database, store by day and then display them.

Warning: I'm using your code for this answer, but DON'T USE mysql, use 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>"
        }
   }
Michel
  • 4,076
  • 4
  • 34
  • 52
  • thank you, but I am getting error: DateTime::__construct() expects parameter 1 to be string – Jakob May 17 '14 at 14:19
  • again this one: DateTime::__construct(): Failed to parse time string (1399872485) at position 8 (8): Unexpected character – Jakob May 17 '14 at 14:24
  • is it because you are getting day with 'date' while it's unix epoch time? – Jakob May 17 '14 at 14:25
  • Try `new DateTime("@$thisday");` – Michel May 17 '14 at 14:29
  • nothing, again error: DateTime::__construct(): Failed to parse time string (@Resource id #11) at position 0 (@): Unexpected character – Jakob May 17 '14 at 14:34