建议使用PHP和MySQL存储当前时间的方法是什么? [英] What's the recommended way to store current time using PHP and MySQL?
本文介绍了建议使用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:
- 使用
gmstrftime
处理gmt - 在列中使用
INT
而不是VARCHAR
- 使用mysql函数
CURTIME
或CURDATE
- 使用
UNIX_TIMESTAMP
mysql函数
- use
gmstrftime
to handle gmt - use
INT
instead ofVARCHAR
for the column - use the mysql function
CURTIME
orCURDATE
- use the
UNIX_TIMESTAMP
mysql function
没有一个使用DATETIME
或TIMESTAMP
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屋!
查看全文