0

I am trying to insert the current date into MySQL database in this format: (12/31/2013 10:26:12 PM). I've tried to make a simple code to change the format, but all I get is a syntax error

$sql = "INSERT INTO Students VALUES ('','" . $info[$i]['firstname'] . "', '" . $info[$i]['lastname'] . "', '" . $info[$i]['sex'] . "', '" . $info[$i]['major'] . "', '" . $info[$i]['favorite'] . "', '" . $info[$i]['GPA'] "TO_CHAR(SYSDATE(),'dd/mm/yyyy')"; 

Tell me please what shall I do with it.

3
  • You are having syntax errors because your syntax is wrong. Your value string starts with a comma, and you need to specify what columns you're inserting into if I'm not mistaken (that part I could be wrong about). Check your actual SQL syntax before assuming your date format is off. Also, what is the format of your date column? Commented Jun 8, 2013 at 21:23
  • Why don't you use the default php time() function? Commented Jun 8, 2013 at 21:24
  • My column datatype is data , I can not use the php time() function cuz I have to change my whole project for that, can you help to change the format of sysdate() function ?? Commented Jun 8, 2013 at 21:34

2 Answers 2

1

Just try this

$sql = "INSERT INTO Students VALUES ('','" . $info[$i]['firstname'] . "', '" . $info[$i]['lastname'] . "', '" . $info[$i]['sex'] . "', '" . $info[$i]['major'] . "', '" . $info[$i]['favorite'] . "', '" . $info[$i]['GPA'] . gmdate('m/d/Y g:i:s A').")"; 

or try this one

$sql = "INSERT INTO Students VALUES ('','" . $info[$i]['firstname'] . "', '" . $info[$i]['lastname'] . "', '" . $info[$i]['sex'] . "', '" . $info[$i]['major'] . "', '" . $info[$i]['favorite'] . "', '" . $info[$i]['GPA'] ."', '" . gmdate('m/d/Y g:i:s A').")"; 

You can also change gmdate with date Have A nice day

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you sir that is right but I had to change the date row datatype to varchar :))
sir how can I change the time to my time zone +3
try to change with this gmdate('m/d/Y g:i:s A', time()+10800)
for any other time zone just calculate 3600 x number of the hours Exemple for 3 hours : 3600*3=10800 and edit ;)
0
USE 
DATE_FORMAT(NOW(),'%m/%d/%Y %h:%i:%s %p') ; i think some error in query also check: $sql = "INSERT INTO Students VALUES ('','" . $info[$i]['firstname'] . "', '" . $info[$i]['lastname'] . "', '" . $info[$i]['sex'] . "', '" . $info[$i]['major'] . "','" . $info[$i]['favorite'] . "', '" . $info[$i]['GPA'] ."',DATE_FORMAT(NOW(),'%m/%d/%Y %h:%i:%s %p') )";
it should work.
check link: http://www.w3schools.com/sql/func_date_format.asp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.