开发者

Format date output in PHP

I'm new to PHP and do not consider myself a coder. I am tackling my first database driven action and am having trouble getting the date output to format properly. The line where I am formatting the date seems to be ignored. Am I doing something incredibly wrong?

This is what outputs on the page:

Comment from: Jeremiah
1289594028

here is my comment

Here is the code:

<?php

    $name = $_POST['name'];
    $comment = $_POST['comment'];


    if($name && $comment and ereg("^[A-Za-z0-9' -]+$",$name) and ereg("^[A-Za-z0-9' -,.:;()!?&#@'/]+$",$comment))

    {
        mysql_query("insert into pilot_comments (name,comment,date,page) values ('$name', '$comment',NOW(),'challenge1')");
    }

    elseif($_POST['submitted']==1) 

    { 
       开发者_如何学Python echo "<img src='images/structure/commenting_spacer.png' height='1' width='249'>";

        echo "<br /><br /><div style='width:209px; background-color:#ff0000; padding:20px'><span class='white bold fourteen'>Uh oh! You may have:<br /><br />(1) Entered special characters, or <br /><br />(2) Not entered text into both fields.</span><br /><br /><span class='white twelve'>(This helps me fight spam. Thanks for understanding.)</span></div><br /><br />";
    }

?>

<?php

    $result = mysql_query("select name,comment,UNIX_TIMESTAMP(date) from pilot_comments where page='challenge1' order by date desc");

    echo mysql_error();

    $date = date("l, F j, Y \a\t g:i:s \P\S\T",$date);

    while(list($name,$comment,$date) = mysql_fetch_row($result))

    {
        echo "<img src='images/structure/commenting_spacer.png' height='1' width='249'>";

        echo "<div STYLE='word-wrap:break-word;width:249px;left:0px'><span class='twelve gray'><br />Comment from: </span><span class='twelve gray bold'>$name</span></div>";

        echo "<span class='ten light-gray bold'>$date<br /><br /></span>";

        echo "<div STYLE='word-wrap:break-word;width:249px;left:0px'><span class='fourteen green bold'>$comment</span></div><br /><br />";
    }
?>


Move this line:

$date = date("l, F j, Y \a\t g:i:s \P\S\T",$date);

inside the while loop (where a new $date is created in every iteration, so your formatting goes lost):

 while(list($name,$comment,$date) = mysql_fetch_row($result))

    {

     $date = date("l, F j, Y \a\t g:i:s \P\S\T",$date);

     ... rest goes here ...

By the way, your code is vulnerable to SQL injection. You should fix that before using the script in real world use by wrapping every mention of $_POST in an escape function like so:

$name = mysql_real_escape_string($_POST["name"]);


It looks like you do not display errors, because with your code, you should at least get a warning for $date, which is undefined when used the first time.

This is like driving without any dashboard.

If you are working on a development environment, make sure errors are displayed (display_errors apache setting), and that your error level is high (set the error_reporting setting to -1 for maximum error output).


You need to move $date = date("l, F j, Y \a\t g:i:s \P\S\T",$date); into the while loop

With it outside of the loop, the value from the database is not getting stored in $date for each iteration of the loop.

You also need to change UNIX_TIMESTAMP(date) to UNIX_TIMESTAMP(date) as date in the query so that the variable gets saved in the result row with the name your code is expecting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜