Store unix timestamp from php file in mysql

I now have this code:

$mysqldate = date(time()); mysql_query("INSERT INTO permissions (date) VALUES ('$mysqldate')"); 

I run away before embedding, but my problem is that time becomes stored as all 0. I was wondering what type of column data in mysql would store something like:

 1311602030 

unix timestamp, and then correctly allow me to order by the latest dates in the request.

+4
source share
4 answers

If the timestamp column in the database is of type INTEGER, you can do

 mysql_query("INSERT INTO permissions (date) VALUES ('".time()."')"); 

As an integer value, you can also perform a sort operation and convert it using the date() function from PHP back to a readable date / time format. If the timestamp column in the database is of type DATETIME, you can do

 mysql_query("INSERT INTO permissions (date) VALUES ('".date('Ymd H:i:s')."')"); 

or

 mysql_query("INSERT INTO permissions (date) VALUES (NOW())"); 
+11
source

Try:

 mysql_query("INSERT INTO permissions (date) VALUES ('".$mysqldate."')"); 
0
source

the date () function needs a string as the first argument, which is the format. time () should be the second argument, for example: date ('Ymd H: i: s', time ()). this will return a row that you can use with the DATETIME column. other than that, if you want to save the unix timestamp, just use the INT column. so that you can save the result of time () directly without calling a date. (or, as I said earlier, format the timestamp with date () with a valid format string and use the DATE, DATETIME or TIMESTAMP column). see http://www.php.net/manual/en/function.date.php and http://dev.mysql.com/doc/refman/5.1/en/datetime.html

EDIT: 0 that you see is because mysql did not recognize the format you used for the column (therefore, if you use the DATE column, but you pass it the wrong format, it is not recognized, so 0 is saved)

0
source

Try:

 $mysqldate = date('Ymd H:i:s'); mysql_query("INSERT INTO permissions (date) VALUES ('$mysqldate')"); 
0
source

All Articles