1

I have a MySQL database. All the fields, I assign and I have a datestamp for my date field. it automatically generates YYYY-MM-DD HH:MM:SS like this 2011-11-21 21:31:37 However, I would like it to do so in two diffrent columns:

A date field with YYYY-MM-DD or 2011-11-21

A time field with HH:MM:SS or 21:31:37

This is my insert php code

$sql= "INSERT INTO  `db`.`table` (`id` ,`fkid` ,`paid` ,`date`)
VALUES (NULL, '$userid', '0', CURRENT_TIMESTAMP);";

I have tried CURRENT_DATESTAMP and it does not work.

The 2nd part of the question is: how to I make the table so it works with the proper code? Should the structure of the of the field be type text, or date?

1 Answer 1

5

I would not recommend splitting your timestamp into separate date and time columns. Instead, it is easiest to use a DATETIME column, and query it for its date and time portions using the MySQL functions DATE() and TIME():

SELECT DATE(`date`) AS d, TIME(`date`) AS t FROM db.dable;

When inserting, you can use the NOW() function to set the current timestamp. `CURRENT_TIMESTAMP() is a synonym for NOW().

$sql= "INSERT INTO  `db`.`table` (`id` ,`fkid` ,`paid` ,`date`)VALUES (NULL ,  '$userid',  '0', NOW());";
Sign up to request clarification or add additional context in comments.

1 Comment

How can I then compare the what d returns to the current date

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.