使用 PHP 和 MySQL 存储当前时间的推荐方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2323945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:02:24  来源:igfitidea点击:

What's the recommended way to store current time using PHP and MySQL?

phpmysqlgmt

提问by yretuta

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:

我遇到了以下方法:

  1. use gmstrftimeto handle gmt
  2. use INTinstead of VARCHARfor the column
  3. use the mysql function CURTIMEor CURDATE
  4. use the UNIX_TIMESTAMPmysql function
  1. 用于gmstrftime处理gmt
  2. 使用INT代替VARCHAR
  3. 使用 mysql 函数CURTIMECURDATE
  4. 使用UNIX_TIMESTAMPmysql函数

none of which were using the DATETIMEor TIMESTAMPmysql var type.

其中没有一个使用DATETIMETIMESTAMPmysql var 类型。

Do you have a better approach for this one?

你有更好的方法吗?

回答by Alexar

It is recommended to use mysql timestamp(YYYY-MM-DD HH:MM:SS) field type to store time and date variables in mysql.

建议使用mysql时间戳(YYYY-MM-DD HH:MM:SS)字段类型在mysql中存储时间和日期变量。

$sDate = date("Y-m-d H:i:s"); // 2015-04-07 07:12:51
mysql_query("insert into `table_name` set `created_on` = '$sDate'");

It gives you ability to use mysql functionsto compare dates, calculate time differences and so, directly in your mysql query.

它使您能够直接在您的 mysql 查询中使用mysql 函数来比较日期、计算时差等。

Also you can always retrieve the timestamp using strtotime()function.

此外,您始终可以使用strtotime()函数检索时间戳。

$result = mysql_query("select `created_on` from `table_name`");
$row = mysql_fetch_row($result);
$iTimestamp = strtotime($row[0]); // 1428390771

回答by casraf

I just use the TIMESTAMPvalue type in MySQL, and let MySQL use its own CURRENT_TIMESTAMP.

我只是TIMESTAMP在 MySQL 中使用值类型,并让 MySQL 使用自己的CURRENT_TIMESTAMP.

回答by Tyler Carter

Well, you can turn a MySQL TIMESTAMP field into a PHP Time() value by using strtotime()

好吧,您可以通过使用将 MySQL TIMESTAMP 字段转换为 PHP Time() 值 strtotime()

Then you just have to make a function that correctly turns a PHP Time() value into a MySQL TIMESTAMP value.

然后,您只需创建一个函数,将 PHP Time() 值正确转换为 MySQL TIMESTAMP 值。

回答by AlSayed Gamal

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.

将其存储为 int 更合乎逻辑。您保存日期的行格式,稍后您可以使用它提取其他格式和更多数据..我也将其保存为int。

Edit: For(Uni-TimeZone) application int is the fastest way and PHP has great time conversion tools.

编辑: For(Uni-TimeZone) application int 是最快的方法,PHP 有很棒的时间转换工具。

回答by álvaro González

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:

MySQL 使用的格式不需要与 PHP 使用的格式匹配。选择两边最好的。数据库有日期数据类型是有原因的;在 MySQL 中,您可以从以下选项中进行选择:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html

In PHP, you basically have three options:

在 PHP 中,您基本上有三个选项:

  • 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).

  • DateTimeobjects 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).

  • 好的旧时间戳很容易处理,你可以在你的 MySQL 查询中使用它们——参见 FROM_UNIXTIME()UNIX_TIMESTAMP()——但是它们有严重的范围问题(你不能依赖它们来处理 1970 年之前的日期,所以它们不合适生日)。

  • DateTime对象功能强大且内置,没有范围问题并支持时区。但是,它们有时使用起来不太舒服,因为它们似乎缺少一些重要的方法。

  • 您可以使用自定义日期对象(基于第三方或您自己的 DateTime)。