Hey Guys, im fairly new to PhP so i dont really know how to type variables, ive been editing php code, but don't really know how it all works yet. Im stuck with timestamp(database) to php date manipulation if thats the right terms which it probably isnt. Anyway ive found a code to manipulate timestamp with php and i tested it on a blank page and the date displays perfectly for each record from the data base and i can also change the format to D, M, Y and so forth. Im trying to get this exact output in a html table column, every time i try to edit the code and try something new it never seems to work quite right, most of the time i get a 1970 date on all records, or it will pull a date from 1 individual record and display that date for all the records.

I need this

<td class="alt"><?php echo $f5; ?></td>

to pick up this date format with echo, cause it seems as long as echo is in front of date it will work perfectly

echo date("g:i a F j, Y ", strtotime($row["time"]));

Help would be greatly appreciated i just hope i explained it clear enough as i am not well schooled in php thanks

<?php
$i=0;
while ($i < $num) {

 $f2=mysql_result($result,$i,"firstname");

 $f3=mysql_result($result,$i,"country");

 $f4=mysql_result($result,$i,"heal");

 mysql_connect("localhost", "root", "pass") or die(mysql_error());

mysql_select_db("mydb") or die(mysql_error());

$result1 = mysql_query("SELECT timestamp FROM light;");
while ( $row = mysql_fetch_array($result1) ) {

   echo date("g:i a F j, Y ", strtotime($row["timestamp"]));

 }
?>

<tr>

<td class="alt"><?php echo $f2; ?></td>
<td class="alt"><?php echo $f3; ?></td>
<td class="alt"><?php echo $f4; ?></td>
<td class="alt"><?php echo $f5; ?></td>

</tr>

<?php
$i++;
}
?>

    Ive tried this as a Variable

    $f5 = date("g:i a F j, Y ", strtotime($row["time"]));

    with this table code

    <td class="alt"><?php echo $f5; ?></td>

    I get the proper date format

    11:13 am October 7, 2011

    for each record the timestamp is the same 11:13 am October 7, 2011 in the php file table, when i check the mysql database the timestamps are all different. It seems that this date 11:13 am October 7, 2011 is being pulled from one of the records in the database and assigning it to all the records in the php file table. What am i doing wrong?

      Hello,

      Are you sure you pasted that correctly? Where is $num set? As it reads, you have bigger problems than a timestamp ... namely, a while() loop that you won't get out of.

      Assuming that $num is set somewhere previous to this code's execution, I'd next want to inquire at the timestamp field in your database. A date of 1-1-1970 is the result of a call to [man]date/man with a time parameter of zero, which in this case means that [man]strtotime/man is returning false, or, in other words, it can't figure out how to translate the value it's getting, or else the value it's getting is, itself, false or zero.

        Thanks for for the response dale, the $num is at the very top there are 2 whiles inside { } im not sure how to configure this to work properly basically i pulled codes from around the web and put them into the code above, there is more to this file should i post the whole code? How can i get out of the while loop?

          If $num is actually set to a number, you can ignore that first comment.

          What are "time" and "timestamp" actually set to for a couple of the rows in the database? I mean, I'm guessing that "timestamp" is an actual UNIX timestamp, similar to "1318020901" (which was one of the seconds that went by as I typed this)? Or is it some kind of MySQL date/time field?

            the format in mysql database looks like this 2011-10-07 11:53:33 i attached a screen shot of mysql database rows for records stored called data.png

              This is what it displays in my table with the code above its reposting ID # 141 over and over

                Sorry im having a rough day its actually Hilary Harding #134

                  If you don't need the timestamp in its original form, then why SELECT it that way at all? MySQL's built-in DATE_FORMAT() function can transform the date into whatever format you like before you even retrieve the row.

                    Hey Brad I read about that over at mysql web site but i dont understand how to configure it or add it to mysql im fairly new if you could help me set up the the DATE Format more detailed i would be greatly appreciated with a short explanation of how it works would be awesome.

                      I found this TIMESTAMP(8) or DATE_FORMAT(cds.bought, '%d-%m-%Y') but im not sure how to use them in mysql can anyone please explain thanks

                        Below is the format i used n mysql

                        SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');

                          this is the output in my table with this

                          $f5=mysql_result($result,$i,"time");
                          
                          
                          <td class="alt"><?php echo $f5; ?></td>

                          Was hoping it would output more like this year/month/day or any combination

                            I just dont know how to use the date format please help im loosing my mind, been working at this for approx 3 days and im ready to sign into a mental asylum

                              omg i figured it out thanks so much dalecosp & bradgrafelman i want to apologize for my craziness but ive been struggling with this for days and i cant tell you how much my head is hurting, i know i should take breaks but when i start something i have to finish it.

                              ~Many Blessings~

                                Write a Reply...