从PHP添加日期到mysql以进行排序 [英] Add date to mysql from php for sorting later

查看:93
本文介绍了从PHP添加日期到mysql以进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP将日期插入到MySQL数据库中时,最好的格式是什么,以便日后能够排序。我开始使用

  $ current_time = date(Y-m-d); 

这是最好的做法吗?

解决方案

如果您能够控制数据库字段,那么建议使用MySQL内置的Timestamp数据类型。您可以默认将其设置为当前时间。

 创建表如果不存在`your_table`(
`date `timestamp NOT NULL default CURRENT_TIMESTAMP
)ENGINE = MyISAM DEFAULT CHARSET = utf8;

如果不是,我会推荐只是将默认的PHP Unix格式的时间戳存储在整数字段中。 p>

  $ current_time = time(); 


when inserting a date into a MySQL DB using PHP what is the best format so that I can sort by date later. I started using

$current_time = date("Y-m-d");

Is this the best practice?

解决方案

If you are able to control your database fields then I would recommend using MySQL's built in Timestamp data type. You can set it to the current time by default.

CREATE TABLE IF NOT EXISTS `your_table` (
    `date` timestamp NOT NULL default CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

If not then I would reccomend just storing the default PHP Unix formatted timestamp in an integer field.

$current_time = time();

这篇关于从PHP添加日期到mysql以进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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