0

Im trying to get the current time in PHP using $sTime = date("Y-m-d H:i:s"); and store it in a mysql table in a datetime column like: $sTQuery = "INSERT INTO Actions(UserID, StartDuration) VALUES ('$UserID' , '$sTime')";

The date echos as expected but in the table it just shows 0000-00-00 00:00:0.

I have tried using now() in the query but still get 0000-00-00 00:00:0 in the table.

Edit: In the mysql table the column the date and time is to be stored in is a datetime

13
  • 2
    what's the column type or table schema? Also, read about PDO or at least about SQL Injection Commented Jan 31, 2018 at 11:43
  • if you use now(), make sure to use that without quotes. Although depending on the column type, you may need a different function like current_timestamp(). Commented Jan 31, 2018 at 11:44
  • Have you looked what has been stored in the database? This is not the same as a date 'print' or 'echo'. Commented Jan 31, 2018 at 11:45
  • 1
    Why is the last zero in 0000-00-00 00:00:0 missing? Commented Jan 31, 2018 at 11:49
  • 1
    Based on the code in your question (which is vulnerable to sql injection), this is not possible. The problem lies elsewhere in your code, but you are not providing enough information. Please edit your question with the create table statement, and the SELECT query you are testing with, so we can try to reproduce the problem. Also provide the result to SELECT NOW(). Commented Jan 31, 2018 at 11:57

1 Answer 1

1

Try to use this query... This contains DateTime value by now() as per your need.

$sTQuery = "INSERT INTO Actions(UserID, StartDuration) VALUES ('$UserID' ,now())";
Sign up to request clarification or add additional context in comments.

7 Comments

Tried this and in the mysql table the entry is still 0000-00-00 00:00:00
what? what your column data type.
the column is a datetime
so you tried the direct query on MySQL without PHP is this query give the same result there too?
Make your column type as TIMESTAMP instead on DateTime may this can solve your issue.
|

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.