Jump to content

Trying to convert set of $_post variables to date to save


learningPHP1

Recommended Posts

hello,

Trying to convert 2 post variables to strtotime so that it can be saved to a database.

The 2 variables are:
$testDate = $_POST['date'] ; // containts: 2014-04-22
$testTime = $_POST['time']; // containt: 21:02:17

echo $date_time = $testDate . $testTime; // result: 6969-1212-3131 19:00:00
echo $new_date = date('yy-mm-dd H:i:s', strtotime($date_time)); // result: 6969-1212-3131 19:00:00

how do i go about setting the $new_date variable to 2014-04-22 21:02:17 so that it can be saved to a database(mysql data type: datetime)

any help you can provide would be greatly appreciated.
Thanks

Link to comment
Share on other sites

What the heck are those echo statements doing?  I've never encountered anyone using them that way before.  Please explain.

 

Other than that you should look up the format specs on the date function since yours are definitely wrong.  (You get '6969' cause you have a spec that begins with 'yy'  which means the last 2 digits of the full year will come out twice.)

Link to comment
Share on other sites

$testDate = $_POST['date'] ; // containts: 2014-04-22

$testTime = $_POST['time']; // containt: 21:02:17

 

echo $date_time = $testDate . $testTime; // result: 6969-1212-3131 19:00:00

No, the result is "2014-04-2221:02:17". Which should explain why strtotime() couldn't understand it.
Link to comment
Share on other sites

There is no need to use date() to reformat the date if your $date_time string is already in the correct format to be handled by your datetime field.

 

The only problem you have is you are not concatenating the $testDate and $testTime variables with a space.

$date_time = $testDate . ' ' . $testTime;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.