5

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.

2
  • Any reason why you don't want to use mysql timestamps? Commented Jul 25, 2011 at 13:58
  • What is the data type of the date column? You are trying to insert an integer. Also, I would highly recommend using a DATETIME or TIMESTAMP column instead of storing integers. You can use the MySQL function NOW() to insert the current time into a DATETIME/TIMESTAMP column. Commented Jul 25, 2011 at 14:04

4 Answers 4

13

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())");
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

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

Comments

1

the date() function needs a string as the 1st argument, which is the format. the time(), should be the 2nd argument, like: date('Y-m-d H:i:s', time()). this will return a string, that you can use with a DATETIME column. other than that, if you want to store a unix timestamp, just use an INT column. so you can store the result of time() directly, without calling date. (or, as i said before, format the timestamp using date() with a valid format string and use a 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: the 0's you are seeing, are there because mysql did not recognice the format you used for the column (so if you are using a DATE column but you're passing it the wrong format, it is not recogniced, so a 0 is saved)

Comments

1

Try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.