加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:33:24 所属栏目:PHP教程 来源:网络整理
导读:我最初的做法是: $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); 我遇到了以
我最初的做法是:
$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的mysql var类型.

你有一个更好的办法吗?

建议使用mysql timestamp(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'");

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

您也可以随时使用strtotime()功能检索时间戳.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读