开发者

Add date to mysql from php for sorting later

开发者 https://www.devze.com 2023-01-30 16:10 出处:网络
when inserting a date into a MySQL DB using PHP what is the best format so that I ca开发者_如何学JAVAn sort by date later. I started using

when inserting a date into a MySQL DB using PHP what is the best format so that I ca开发者_如何学JAVAn 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();


The database handles dates internally for storing and sorting. Y-m-d format is good


You do not ever need to generate the current date or time in PHP to insert it into a query. Use the MySQL constants CURRENT_DATE and CURRENT_TIMESTAMP in the query instead.

INSERT INTO table (name, date) VALUES ('Bob', CURRENT_DATE)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号