alsabsab
alsabsab

Reputation: 1321

change the format of the date inserted by PHP into MySQL database

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.

Upvotes: 0

Views: 740

Answers (2)

user1312617
user1312617

Reputation:

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

Upvotes: 0

Alpha Root Mks
Alpha Root Mks

Reputation: 114

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

Upvotes: 1

Related Questions