使用 PHP 和 MySQL 存储当前时间的推荐方法是什么?
我最初的方法是:
$current = time(); // save this to column CURRENT_TIME with column type VARCHAR
//retrieve it like this
$retrieved = mysql_query(....) //assume query for getting the stored time value
$time = strtotime($retrieved);
我遇到了以下方法:
- 使用
gmstrftime
来处理gmt, - 使用
INT
而不是VARCHAR
作为列, - 使用mysql函数
CURTIME
或CURDATE
- 使用
UNIX_TIMESTAMP
mysql 函数
,但没有使用 DATETIME
或 TIMESTAMP< /code> mysql var 类型。
您对此有更好的方法吗?
My initial approach was:
$current = time(); // save this to column CURRENT_TIME with column type VARCHAR
//retrieve it like this
$retrieved = mysql_query(....) //assume query for getting the stored time value
$time = strtotime($retrieved);
I have come across the following approaches:
- use
gmstrftime
to handle gmt - use
INT
instead ofVARCHAR
for the column - use the mysql function
CURTIME
orCURDATE
- use the
UNIX_TIMESTAMP
mysql function
none of which were using the DATETIME
or TIMESTAMP
mysql var type.
Do you have a better approach for this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
建议使用mysql timestamp (YYYY-MM-DD HH:MM:SS)字段类型在mysql中存储时间和日期变量。
它使您能够使用 mysql 函数 直接在 mysql 查询中比较日期、计算时间差异等。
此外,您始终可以使用 strtotime() 函数检索时间戳。
It is recommended to use mysql timestamp (YYYY-MM-DD HH:MM:SS) field type to store time and date variables in mysql.
It gives you ability to use mysql functions to compare dates, calculate time differences and so, directly in your mysql query.
Also you can always retrieve the timestamp using strtotime() function.
我只是在MySQL中使用
TIMESTAMP
值类型,并让MySQL使用自己的CURRENT_TIMESTAMP
。I just use the
TIMESTAMP
value type in MySQL, and let MySQL use its ownCURRENT_TIMESTAMP
.好吧,您可以使用
strtotime()
将 MySQL TIMESTAMP 字段转换为 PHP Time() 值。然后您只需创建一个函数,将 PHP Time() 值正确转换为 MySQL TIMESTAMP 值。
Well, you can turn a MySQL TIMESTAMP field into a PHP Time() value by using
strtotime()
Then you just have to make a function that correctly turns a PHP Time() value into a MySQL TIMESTAMP value.
MySQL 使用的格式不需要与 PHP 使用的格式匹配。选择两边最好的。数据库具有日期数据类型是有原因的;在 MySQL 中,您可以从以下选项中进行选择:
http: //dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html
在 PHP 中,您基本上有三个选择:
好的旧时间戳很容易处理,并且您可以可以在 MySQL 查询中使用它们 - 请参阅 FROM_UNIXTIME() 和 UNIX_TIMESTAMP() - 但它们有严重的范围问题(您不能依赖它们来计算 1970 年之前的日期,因此它们不适合生日)。
DateTime 对象功能强大且内置,没有范围问题并支持时区。然而,它们有时使用起来不太舒服,因为它们似乎缺少一些重要的方法。
您可以使用自定义日期对象(基于第三方或您自己的 DateTime)。
The format used by MySQL doesn't need to match the format used by PHP. Choose the best on either side. Databases have date data types for a reason; in MySQL you can choose from these:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html
In PHP, you basically have three options:
Good old timestamps are easy to handle and you can use them in your MySQL queries —see FROM_UNIXTIME() and UNIX_TIMESTAMP()— but they have serious range issues (you can't rely on them for pre-1970 dates, so they are unsuitable for birthdays).
DateTime objects are powerful and builtin, have no range issues and support time zones. However, they are sometimes not very comfortable to use since they seem to lack some important methods.
You can use a custom date object (third-party or your own DateTime based).
将其存储为 int 更符合逻辑。
您保存日期的行格式,稍后可以使用它提取其他格式和更多数据..
我也把它保存为int。
编辑: For(Uni-TimeZone) application int 是最快的方法,PHP 有很棒的时间转换工具。
Storing it as int is more logical.
You save the row format of date, using which you can later extract other format and more data ..
I also save it as int.
Edit: For(Uni-TimeZone) application int is the fastest way and PHP has great time conversion tools.