我一直在寻找是否已经对此有任何答案,而我还没有找到可以回答我的问题的答案。关于php,我是一个菜鸟,所以请保持友好。
我正在尝试制作一个CMS,到目前为止,我所能做的就是发布帖子,但是当我尝试存储帖子的时间时,它只会输出1969年12月31日。我假设我不存储该帖子。值正确,因为如果显示该日期,则表示计时器为0。

这是我到目前为止所拥有的。

<?php
require('config.php');

if (isset($_POST['name']) AND isset($_POST['mail']) AND isset($_POST['avatar']) AND isset($_POST['title']) AND isset($_POST['message']))
{
$name = addslashes($_POST['name']);
$mail = addslashes($_POST['mail']);
$avatar = addslashes($_POST['avatar']);
$title = addslashes($_POST['title']);
$message = addslashes($_POST['message']);
// We try to find out if we were going to modify the news ...
if ($_POST['id_news'] == 0)
{
// No modifs ?? lets put some data on the tables :p
mysql_query("INSERT INTO news VALUES('', '" . $name . "', '" . $title . "', '" . $message . "', '" . time() . "', '" . $mail . "', '" . $avatar . "')");
}
else
{
// If we want to modify, let's just update all the data
mysql_query("UPDATE news SET name='" . $name . "', title='" . $title . "', message='" . $message . "', mail='" . $mail . "', avatar='" . $avatar . "' WHERE id=" . $_POST['id_news']);
}
}

/*
Verify if ever we want to delete the news
*/
if (isset($_GET['delete_news']))
{
// let's delete data Very Happy
mysql_query('DELETE FROM news WHERE id=' . $_GET['delete_news']);
}
?>

<table width="487">
<tr>
<th>Modify</th>
<th>Delete</th>
<th>Title</th>
<th>Date</th>
<th>Author</th>
</tr>

<?php
$res = mysql_query('SELECT * FROM news ORDER BY id DESC');
while ($data = mysql_fetch_array($res))
{
?>

<tr>
<td align="center"><?php echo '<a href="write_news.php?modify_news=' . $data['id'] . '">'; ?>Modify</a></td>
<td align="center"><?php echo '<a href="list_news.php?delete_news=' . $data['id'] . '">'; ?>Delete</a></td>
<td align="center"><?php echo stripslashes($data['title']); ?></td>
<td align="center"><?php echo date('d F Y - h:i:s a', $data['time']); ?></td>
<td align="center"><?php echo stripslashes($data['name']); ?></td>
</tr>

<?php
}
?>


先感谢您。

最佳答案

您可以执行strtotime函数或执行爆炸,并对爆炸元素执行mktime。

strtotime可能更容易

date('d F Y-h:i:s a',strtotime($ date ['time]));

正如答复中提到的那样,在打印输出变量时,您将需要对其进行Santize处理,并可能保护其输入(而不是加号)。

http://www.php.net/manual/en/function.strtotime.php

http://www.php.net/manual/en/function.date.php

这是转义字符串的链接,但是您可能要考虑最好的选择,那就是对插入内容进行参数化

http://www.php.net/manual/en/mysqli.real-escape-string.php

关于php - 如何使用MySQL和PHP存储时间戳并将其输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20005519/

10-11 01:57
查看更多