Philip Kirkbride Philip Kirkbride - 9 days ago 4
MySQL Question

Using STR_TO_DATE() before pushing to Database

I'm pushing values into a MySQL database I need to take the code I have now and integrate the STR_TO_DATE() function.

The STR_TO_DATE() function needs to be applied to both $start & $end.

$qstring = "INSERT INTO tbl_events VALUES(NULL,'".$name."','".$day."','".$start."','".$end."','".$location."','".$description."','".$type."')";
$result = mysql_query($qstring);

Date Format is "08/03/2011 09:05"

I have already tried replacing $start with STR_TO_DATE($start) it kills the php script.


Lose the quotes around STR_TO_DATE() (it's a function, not a string) and add them around $start (that needs to be a string). Then add the second parameter as Mihai suggests.

$qstring = "INSERT INTO tbl_events 
  STR_TO_DATE( '$start', '%m/%d/%Y %h:%i' ), 
  STR_TO_DATE( '$end', '%m/%d/%Y %h:%i' ), 
$result = mysql_query($qstring) or die( mysql_error() );

Note that you don't need to do "INSERT... '".$var."'...", it's more readable and less error prone if you do "INSERT ... '$var' ..."