×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

sprintf() with date string

sprintf() with date string

sprintf() with date string

(OP)
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; 

RE: sprintf() with date string

Just use a single '%s' as placeholder. The date() function already takes care of the formatting.

The way you have constructed the sprintf is to require 6 arguments for the date piece (one for each of y m d h mins and secs) but you have only supplied one (the preformatted date).

RE: sprintf() with date string

(OP)
I had tried that originally but it failed although it works now! Thank you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close