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

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.)

$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.

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.