dongsuishou8039 2010-11-12 20:36
浏览 29
已采纳

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) 

    { 
        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 />";
    }
?>
  • 写回答

3条回答 默认 最新

  • dongqiaolong9034 2010-11-12 20:43
    关注

    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"]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误