建议使用PHP和MySQL存储当前时间的方法是什么? [英] What's the recommended way to store current time using PHP and MySQL?

查看:74
本文介绍了建议使用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);

我遇到过以下几种方法:

I have come across the following approaches:

  1. 使用gmstrftime处理gmt
  2. 在列中使用INT而不是VARCHAR
  3. 使用mysql函数CURTIMECURDATE
  4. 使用UNIX_TIMESTAMP mysql函数
  1. use gmstrftime to handle gmt
  2. use INT instead of VARCHAR for the column
  3. use the mysql function CURTIME or CURDATE
  4. use the UNIX_TIMESTAMP mysql function

没有一个使用DATETIMETIMESTAMP mysql var类型.

none of which were using the DATETIME or TIMESTAMP mysql var type.

您对此有更好的方法吗?

Do you have a better approach for this one?

推荐答案

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

It is recommended to use mysql timestamp (YYYY-MM-DD HH:MM:SS) field type to store time and date variables in 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'");

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

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

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

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

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

这篇关于建议使用PHP和MySQL存储当前时间的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆