Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store unix timestamp from php file into mysql

Tags:

php

mysql

Right now I have this code:

$mysqldate = date(time());

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

I escape before I insert also, but my problem is that the time is getting stored as all 0s. I was wondering what column datatype in mysql would store something like:

1311602030

a unix timestamp, and then would properly allow me to order by most recent dates on a query.

like image 323
re1man Avatar asked Dec 16 '22 11:12

re1man


2 Answers

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

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

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

mysql_query("INSERT INTO permissions (date) VALUES ('".date('Y-m-d H:i:s')."')");

or

mysql_query("INSERT INTO permissions (date) VALUES (NOW())");
like image 92
rabudde Avatar answered Dec 19 '22 08:12

rabudde


Try:

mysql_query("INSERT INTO permissions (date) VALUES ('".$mysqldate."')");
like image 39
tomthorgal Avatar answered Dec 19 '22 07:12

tomthorgal