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 t开发者_如何学Cbl_events VALUES(NULL,'".$name."','".$day."','".$start."','".$end."','".$location."','".$description."','".$type."')";
$result = mysql_query($qstring);
Date Format is "08/03/2011 09:05"
I've 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
VALUES(NULL,'$name','$day',
STR_TO_DATE( '$start', '%m/%d/%Y %h:%i' ),
STR_TO_DATE( '$end', '%m/%d/%Y %h:%i' ),
'$location','$description','$type')";
$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' ..."
What is your $start
and $end
format ?
STR_TO_DATE('01,5,2013','%d,%m,%Y')
STR_TO_DATE('May 1, 2013','%M %d,%Y')
those are 2 ways ... it all depends on your date format
精彩评论