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:00am
The
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
%pto denote theAM/PMportion of the format. If you add that to your function call, it should work.Try this: