11 月 30 日 -0001 返回时间戳

发布于 2024-12-10 23:54:13 字数 658 浏览 0 评论 0原文

在有人说是这样之前,我检查了其他类似的问题,但无济于事。基本上,我从数据库中获取时间戳并显示它,这是用于检索的代码:

while($row = mysql_fetch_array($result)) {
    $timestamp = date("l F d Y", strtotime($row['timestamp']));
}

这是提交时的代码:

$sql = "INSERT INTO mysql_table (timestamp) VALUES (now())";

我认为这将是一个简单的格式错误(因此是 strtotime),但事实并非如此不工作,如您所见: http://aviatex14.co.uk/anonpost/index.php< /a>

我可以将其更改为:

$date = date("l F d Y", $row['timestamp']);

并获取 1969 年 12 月 31 日,但这也没有帮助..

哦,我的列数据类型是日期时间。

任何帮助将不胜感激! :)

Before anyone say's it, I checked other questions like this, but to no avail. Basically, I'm taking a timestamp from my DB and displaying it, here's the code for retrieval:

while($row = mysql_fetch_array($result)) {
    $timestamp = date("l F d Y", strtotime($row['timestamp']));
}

And here is the code for when it is submitted:

$sql = "INSERT INTO mysql_table (timestamp) VALUES (now())";

I thought it would be a simple formatting error (thus the strtotime) but that didn't work as you can see: http://aviatex14.co.uk/anonpost/index.php

I can change it to:

$date = date("l F d Y", $row['timestamp']);

and get December 31st 1969, but that's not heplful either..

Oh, my column data type is datetime.

Any help would be much appreciated! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌梦深 2024-12-17 23:54:13

您必须在单词 timestamp 两边加上反引号,因为这是一个保留字。

 "INSERT INTO mysql_table (`timestamp`) VALUES (now())";

You have to put backticks around the word timestamp as this is a reserved word.

 "INSERT INTO mysql_table (`timestamp`) VALUES (now())";
清泪尽 2024-12-17 23:54:13

如果您打算在 PHP 中进行日期格式化,那么只需从 MySQL 获取 UNIX_TIMESTAMP,它可以直接用作 PHP 时间戳:

SELECT UNIX_TIMESTAMP(`timestamp`) FROM  ...

您的 date(... $row['timestamp'])< /code> 失败,因为 date() 函数需要一个 TIME VALUE,它只是一个 unix 时间戳 - 您传递的是日期的字符串表示形式,它很可能会转换为 0 ,这是简1/1970 00:00,然后根据时区进行调整,因此您会得到 Dec 31/69。

If you're going go to be doing the date formatting in PHP, then just fetch a UNIX_TIMESTAMP from MySQL, which is directly usable as a PHP timestamp:

SELECT UNIX_TIMESTAMP(`timestamp`) FROM  ...

Your date(... $row['timestamp']) fails because the date() function expects a TIME VALUE, which is just a unix time stamp - you're passing in the string representation of a date, which gets converted to 0, most likely, which is Jan 1/1970 00:00, and then adjusted for timezones, so you get Dec 31/69 instead.

薔薇婲 2024-12-17 23:54:13

问题是php使用UNIX_TIMESTAMP,它与MySQL的日期时间格式不匹配。

要么做

SELECT date_format(`timestamp`,aformatstring, see link) as formateddate_as_string

要么做

SELECT UNIX_TIMESTAMP(`timestamp`) as php_friendly_timestamp 

http:// /dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

The problem is that php uses UNIX_TIMESTAMP, which does not match with MySQL's datetime format.

Either do

SELECT date_format(`timestamp`,aformatstring, see link) as formateddate_as_string

Or do

SELECT UNIX_TIMESTAMP(`timestamp`) as php_friendly_timestamp 

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文