日期格式时间以前

时间:2012-11-29 22:28:56

标签: php

我还是PHP的初学者,但我需要帮助。我有一个评论的日期格式,我正在尝试插入“超时”格式。我遇到了麻烦。这是代码:

<?php
$name = mysql_real_escape_string($_POST['name']);
$comment = mysql_real_escape_string($_POST['comment']);
$submit = $_POST['submit'];
$date = date("M jS Y | g:i a T");
$ip = "".$_SERVER['REMOTE_ADDR']."";

if($submit)
{
if($name&&$comment)
{

$query=mysql_query("

INSERT INTO comment (id, articleid, name, comment, date, ip) VALUES ('','".  mysql_real_escape_string($_GET['id']) ."','$name','$comment','$date','$ip')

");
header("Location: " . mysql_real_escape_string($_GET['id']) . "");

}
else
{
    echo "<div class='red'>You must fill in all fields!</div>";
}

} ?&GT;

这是输出:

<div class="article-body">
<div class="user"><b><?php echo $row['name'] ?></b> <span>says:</span></div> 
<div class="comments">
<?php echo $row['comment'] ?> <br /><p></p>

<p style="font-size:12px;">Posted on <b><?php echo $row['date'] ?></b></p>
</div>
</div>

我尝试使用Google来摆弄几个代码,但没有成功,因为我是一个菜鸟。哈哈。

对此有何看法?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果没有看到您的MySQL表架构/数据类型,我猜您的date数据是无效的date值。 (我假设您的date列不是TEXTVARCHAR

$date = date("M jS Y | g:i a T");

您可以向查询添加or die(mysql_error)以验证此内容 -

$query=mysql_query("INSERT INTO comment (id, articleid, name, comment, date, ip) VALUES ('','".  mysql_real_escape_string($_GET['id']) ."','$name','$comment','$date','$ip')") or die(mysql_error);

根据手册 - http://dev.mysql.com/doc/refman/5.6/en/datetime.html,您的日期数据类型为datedatetimetimestamp

date => 'YYYY-MM-DD' => set in php using date("Y-m-d");
datetime => 'YYYY-MM-DD HH:MM:SS' => set in php using date("Y-m-d H:i:s");
timestamp => 0000000000 => set in php using strtotime(date("Y-m-d H:i:s"));