Jump to content

Recommended Posts

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>";
}

Link to comment
https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196388
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196390
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196783
Share on other sites

$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>";
}

Link to comment
https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196807
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/232595-date-time-question/#findComment-1196969
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.