MySQL 日期时间转换为 PHP

发布于 2024-09-24 04:37:29 字数 462 浏览 3 评论 0原文

我已经找到了解决我的“问题”的正确方法,但即使在阅读了 mysql 页面之后,我也不明白其背后的逻辑。

目前,我将注册信息存储在我的系统中的一个表 (YYYY-MM-DD hh:mm:ss) 的“日期时间”格式字段中。

当我想在我的 php 页面之一上显示数据时,只需发布​​确切的字段数据即可显示上述格式。

我想简单地使用 date("Ymd",$row["DATE"]) 其中 $row["DATE"] 对应于特定的行值将返回所需的格式。

相反,我必须使用:date("Ymd", strtotime($row["DATE"]))

这是为什么呢?我的 $row["DATE"] 字段首先不是字符串。我应该能够简单地重新排列存储在日期时间字段中的数据吗?这不是重建整个表集以适应日期时间的目的吗?

I have found a proper solution to my "problem" but even after reading mysql pages, I don't understand the logic behind it.

I currently store registration information in my system in a "datetime" formatted field in one of my tables (YYYY-MM-DD hh:mm:ss).

When I want to display the data on one of my php pages, simply posting the exact field data shows the format mentioned above.

I would THINK simply using date("Y-m-d",$row["DATE"]) where $row["DATE"] corresponds to the particular row value would return the desired format.

Instead I have to use:date("Y-m-d", strtotime($row["DATE"])).

Why is this? My $row["DATE"] field is not a string in the first place. Should I be able to simple rearrange the data stored in a datetime field? Wasn't that the purpose of rebuilding my entire tableset to accomodate datetime?

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

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

发布评论

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

评论(2

殤城〤 2024-10-01 04:37:29

MySQL 有一个内置函数,名为 date_format 您可以使用它来显示您想要的日期。

SELECT DATE_FORMAT(date_field, '%Y-%m-%d') as date_field FROM table_name

该手册提供了格式列表以及以这种方式显示所需的变量。使用这种方法,不需要 PHP 对其进行转换等。此外,PHP 端的代码较少,MySQL 可以轻松处理。

编辑

抱歉,刚刚读到您正在寻找解释。

PHP 的 date 函数采用 UNIX 时间戳,而 MySQL 不使用该时间戳。 MySQL 使用真实的日期格式 IE:YYYY-MM-DD HH:MM:SS,如您所知,这是为了在多年后兼容。 UNIX 时间戳的有效范围有限,例如从 1969 年到 2037 年,这使得它对于诸如聊天框消息或预计不会在这些日期后出现的项目等项目的“时间戳”非常有用,其中因为 MySQL DATETIME 不应该消失,直到年份变为 5 位数字或世界末日。

有关详细信息,请阅读 UNIX 时间戳 上的 WIKI。

MySQL has a built in function called date_format which you can use to display the date how you want to.

SELECT DATE_FORMAT(date_field, '%Y-%m-%d') as date_field FROM table_name

The manual has the list of formats and the variables needed to display it that way. Using this method there will be no need to have PHP convert it etc. Plus it is less code on PHP side for something MySQL can handle easily.

EDIT

Sorry, just read you were looking for an explanation.

PHP's date function takes in a UNIX timestamp, which MySQL is not using. MySQL uses a real date format IE: YYYY-MM-DD HH:MM:SS, as you know, this is to be compliant for years later. The UNIX timestamp has a limited range from something like 1969 to 2037 that it is valid for, which makes it really useful for "timestamping" of items such as a chat box message or items they are not expected to be around post those dates, where as the MySQL DATETIME should not die out until the year changes to 5 digits or the world ends.

Read the WIKI on UNIX timestamp for more information on it.

萌︼了一个春 2024-10-01 04:37:29

MySQL 确实允许您选择 unix 时间戳格式的日期,这使得它们可以更轻松地在 PHP 中使用,完全按照您的要求。

前面的答案似乎忽略了这一点,或者由于unix时间戳的范围限制而淡化了它,但如果它是您正在寻找的...

SELECT UNIX_TIMESTAMP(datefield) as u_datefield FROM table

将为您提供时间戳格式的日期,您可以按照您的建议使用它PHP 中:

<?php
$showdate = date("Y-m-d",$row['u_datefield']);
?>

正如前面的答案所暗示的,unix 时间戳确实有一个有限的范围,所以如果您需要 1970 年之前或 2038 年之后的日期,它可能不合适,但对于今天的日常使用来说它很棒。

与日期字符串相比,使用时间戳的主要优点是可以添加和减去时间戳,这对于字符串格式的日期来说要困难得多。

MySQL does allow you to select dates in unix timestamp format, which allows them to be used more easily in PHP, exactly as you requested.

The previous answer seemed to ignore this point, or downplay it due to the range restriction on the unix timestamp, but if it's what you're looking for...

SELECT UNIX_TIMESTAMP(datefield) as u_datefield FROM table

will give you the date in timestamp format, which you can use as you suggested in PHP:

<?php
$showdate = date("Y-m-d",$row['u_datefield']);
?>

As the previous answer suggests, unix timestamps do have a limited range, so if you need dates prior to 1970 or after 2038 it may not be suitable, but for everyday use today it's great.

The main advantage of using timestamps over date strings is that timestamps can be added and subtracted, which is much harder with a date in string format.

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