brenda Posted April 3, 2011 Share Posted April 3, 2011 Hi, I am new to this forum as of now. I have a question which probably has a very straightforward answer but I just can't solve it. I am using mysql and have the following code: while($row=mysqli_fetch_array($result)) { $time1=date("j F Y G: i", $row[1]); echo "<tr><td>$row</td>"; echo "<td>$time1</td>"; echo "<td>$row[comment]</td></tr>"; } It is the first line in the curly brackets that is causing the problem. If I replace $row[1] with $time I get today's date/time. But I need the date/time from what has been input so that won't work. Can anyone explain to me what is wrong with my code please? It gives the following error message: Notice: A non well formed numeric value encountered in C:\xampp\htdocs Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/ Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 Have you verified that $row[1] is a valid timestamp? i.e. something like 1301868624 If it's coming from a DATE-formatted MySQL column then you need to first convert it: $time1 = date("j F Y G: i", strtotime($row[1])); Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196357 Share on other sites More sharing options...
brenda Posted April 3, 2011 Author Share Posted April 3, 2011 Thanks. When I insert that code then the date/time I get is 0000-00-00 Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196359 Share on other sites More sharing options...
Pikachu2000 Posted April 3, 2011 Share Posted April 3, 2011 In what format is the date stored in the DB table? Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196362 Share on other sites More sharing options...
brenda Posted April 3, 2011 Author Share Posted April 3, 2011 Pikachu2000: I am not sure. I am very new to this stuff so forgive me if I am being thick. When I created the table I set up the date column as a date - is that what you mean? Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196372 Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 Ok, so what does $row[1] look like without converting it in any way? Just echo $row[1] and see what it looks like. Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196374 Share on other sites More sharing options...
Pikachu2000 Posted April 3, 2011 Share Posted April 3, 2011 Yes, exactly. Post the database query you're using also please. Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196375 Share on other sites More sharing options...
brenda Posted April 3, 2011 Author Share Posted April 3, 2011 When I echo out row[1} I get a string of zeros again. I am attaching my full php code if that helps. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196379 Share on other sites More sharing options...
Pikachu2000 Posted April 3, 2011 Share Posted April 3, 2011 What is the name of the database table field that the date is in? Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196386 Share on other sites More sharing options...
brenda Posted April 3, 2011 Author Share Posted April 3, 2011 date Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196387 Share on other sites More sharing options...
Pikachu2000 Posted April 3, 2011 Share Posted April 3, 2011 This should work. If not, make sure the database has correct values in it. $sql="SELECT DATE_FORMAT(`date`, '%e %b, %Y') as `formatted_date`, `email`, `comment` FROM comments"; $result=mysqli_query($conn, $sql) or die("Error selecting -".mysqli_error($conn)); while($row=mysqli_fetch_assoc($result)) { echo "<tr><td>$row[email]</td>"; echo "<td>$row['formatted_time']</td>"; echo "<td>$row[comment]</td></tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196388 Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 This should work. If not, make sure the database has correct values in it. $sql="SELECT DATE_FORMAT(`date`, '%e %b, %Y') as `formatted_date`, `email`, `comment` FROM comments"; $result=mysqli_query($conn, $sql) or die("Error selecting -".mysqli_error($conn)); while($row=mysqli_fetch_assoc($result)) { echo "<tr><td>$row[email]</td>"; echo "<td>$row['formatted_time']</td>"; echo "<td>$row[comment]</td></tr>"; } I think you meant this: echo "<td>$row['formatted_date']</td>"; instead of this: echo "<td>$row['formatted_time']</td>"; Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196390 Share on other sites More sharing options...
Pikachu2000 Posted April 3, 2011 Share Posted April 3, 2011 Uh, yup. Sure did! Good catch. Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196392 Share on other sites More sharing options...
brenda Posted April 4, 2011 Author Share Posted April 4, 2011 Thanks so much guys. I have inserted the suggested code and now get the following error message: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs This relates to: echo "<td>$row['formatted_date']</td>"; Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196783 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 $sql="SELECT DATE_FORMAT(`date`, '%e %b, %Y') as `formatted_date`, `email`, `comment` FROM comments"; $result=mysqli_query($conn, $sql) or die("Error selecting -".mysqli_error($conn)); while($row=mysqli_fetch_assoc($result)) { echo "<tr><td>{$row['email']}</td>"; echo "<td>{$row['formatted_date']}</td>"; echo "<td>{$row['comment']}</td></tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196807 Share on other sites More sharing options...
jcbones Posted April 5, 2011 Share Posted April 5, 2011 So you should currently have. <html> <head> <title>sss05</title> <style type="text/css"> body{color:blue;background-color:cornsilk} </style> </head> <body> <h1>Comments on my web site.</h1> <?php //script to display comments on my web site & add a new one //check to see if there is a comment to add $error = NULL; if($_GET["comment"]) { $comment = $_GET["comment"]; $email = $_GET["email"]; $time=date('Y-m-d'); //formatted date, as 4 digit Year, 2 digit month, 2 digit day. for insertion into a date column. $conn=mysqli_connect("localhost", "root", "root", "CommentsDB") or die("Unable to connect to database"); $sql="INSERT INTO comments VALUES('$email','$time', '$comment')"; $result=mysqli_query($conn, $sql); if(mysqli_affected_rows($conn)!==1) { $error = "Error - record not inserted"; } } echo "<div>$error</div><table border='1'><tr><td>Email</td><td>Date</td><td>Comment</td></tr>"; $sql="SELECT DATE_FORMAT(`date`, '%e %b, %Y') as `formatted_date`, `email`, `comment` FROM comments"; $result=mysqli_query($conn, $sql) or die("Error selecting -".mysqli_error($conn)); while($row=mysqli_fetch_assoc($result)) { echo "<tr><td>{$row['email']}</td>" ."<td>{$row['formatted_date']}</td>" ."<td>{$row['comment']}</td></tr>"; } ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.