Problems editing Time Value with type conversion
I'm using PHP & MySQL to go into a database and change the value of a date. A conversion of date format is required going from the user interface to the database. The code I'm using to convert and push to database is...
<?php
require_once("../../connect.php");
$e = $_GET['e'];
$start = mysql_real_escape_string($_POST['start']);
$updatestring = "UPDATE tbl_events SET start=STR_TO_DATE('".$start."', '%m/%d/%Y %h:%i' )
WHERE id=".$e;
$update_results = mysql_query($updatestring);
header("Location: event.php?e=".$e);
?>
When the value is put through the script all pm events are converted to am.
EXAMPLE
INPUT= 08/20/2011 04:00 pm
OUTPUT= 08/20/2011 4开发者_C百科:00amThe
start=STR_TO_DATE('".$start."', '%m/%d/%Y %h:%i' )
part of the code works fine in my create event script. Can anyone tell me why its not working in this situation?
Essentially, you're passing more data to MySQL than you're explaining what to do with. You're passing "8/20/2011 04:00 pm", but only giving enough information to interpret the "8/20/2011 04:00" portion. Since MySQL doesn't know what to do with the "pm", it just ignores it and makes a bad assumption, defaulting to AM.
MySQL uses %p
to denote the AM/PM
portion of the format. If you add that to your function call, it should work.
Try this:
start=STR_TO_DATE('".$start."', '%m/%d/%Y %h:%i %p' )
select str_to_date('08/20/2011 04:00 pm','%m/%d/%Y %I:%i %p') -- 2011-08-20 16:00:00
gives the right datetime.
精彩评论