PCHomepage
Programmer
I'm trying to insert a date/time to MySQL using sprintf() but can't seem to work out how to do it. The date and time are formatted as 2014-10-01 09:30:00 but I'm not sure how to format the data to insert it. I tried '%04d-%02d-%02d %02d:%02d:%02d' but get an error of too few arguments. What am I doing wrong?
Code:
$PostID = (isset($_POST["ID"]) && !empty($_POST["ID"])) ? $_POST["ID"] : "";
$Title = (isset($_POST['Title'])) ? $_POST['Title'] : NULL;
$StartDate = (isset($_POST['StartDate'])) ? $_POST['StartDate'] : NULL;
$StartTime = (isset($_POST['StartTime'])) ? $_POST['StartTime'] : NULL;
$FormattedDateTime = "$StartDate $StartTime";
$FormattedDateTime = date('Y-m-d H:i:s', strtotime($FormattedDateTime));
// Insert record
if (isset($_POST) && (isset($_POST["insert"]) || isset($_POST["update"]))) :
$sqlInsert = sprintf("INSERT INTO calendar (Title, Start)
VALUES ('%s','%04d-%02d-%02d %02d:%02d:%02d')",
$Title,
$FormattedDateTime);
$PostID = DBConnect($sqlInsert, "Insert", "dbname");
$Message = "A new calendar entry has been created.";
endif;