从mysql数据库中检索时,日期/时间格式不正确 [英] Incorrect Date/Time Formatting when retrieving from mysql database
问题描述
我正在更新遗留Web应用程序,以包含一个提交的php表单,在MySQL数据库的表中插入日期时间戳。数据库中的日期格式显示为:2015-05-18 14:34:23。
但是,当我在Web应用程序中显示日期时,它会返回:
1969-12-31 19:33:35显然,默认日期(1969年12月31日)表示格式错误...我不知道如何纠正。
sql代码如下:
I'm updating a legacy Web application to include a php form that once submitted inserts a date time stamp into a table in a MySQL database. The date format in the database is shown as: 2015-05-18 14:34:23.
However, when I display the date in the Web application it comes back as:
1969-12-31 19:33:35. Obviously, the default date of (Dec. 31, 1969) indicates a formatting error... which I'm not sure how to correct.
The sql code is as follows:
$title = mysql_real_escape_string($_POST['tofiles_title']);
$body = mysql_real_escape_string($_POST['tofiles_body']);
$link = "http://example.com/uploads/" . mysql_real_escape_string($_POST['tofiles_link']);
$relation = mysql_real_escape_string($_POST['tofiles_relation']);
$type = mysql_real_escape_string($_POST['tofiles_type']);
$date = date('Y-m-d H:i:s', time());
//$date = mysql_real_escape_string($_POST['tofiles_post_date']);
$ip = $_SERVER['REMOTE_ADDR'];
$post_user = $_SESSION['user_id'];
$sql = "INSERT INTO site_tofiles (tofiles_title, tofiles_body, tofiles_link, tofiles_relation, tofiles_type, tofiles_post_date, tofiles_post_ip, tofiles_post_user) VALUES ";
$sql .= "('$title', '$body', '$link', '$relation', '$type', '$date', '$ip', '$user_id');";
mysql_query($sql);
注意:遗留Web应用程序帖子中的其他位置日期以下列格式显示:2015年5月13日,下午4:30,MySQL数据库中的日期显示为:1422475338。如果我没有弄错的话,我认为这是日期的UNIX代码。
NOTE: Elsewhere in the legacy Web application post dates are shown in the following format: May 13, 2015, 4:30 pm, and the date in the MySQL database is shown as: 1422475338. I think this is the UNIX code for date if I'm not mistaken.
推荐答案
title = mysql_real_escape_string(
title = mysql_real_escape_string(
_POST [' tofiles_title']);
_POST['tofiles_title']);
body = mysql_real_escape_string(
body = mysql_real_escape_string(
这篇关于从mysql数据库中检索时,日期/时间格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!