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
I am curious to know how you get $_POST[‘date’] and $_POST[‘time’]? If it is up to the user then I should imagine there could be variations that would cause problems.
Also if the current date and time are required to be saved then MySQL has a novel field attribute where the timestamp is saved automatically. [TABLE=“class: data”]
[TR=“class: odd”]
[TD=“class: nowrap”]CURRENT_TIMESTAMP[/TD]
[TD=“class: nowrap”]ON UPDATE CURRENT_TIMESTAMP[/TD]
[/TR]
[/TABLE]
Using this feature, the timestamp can be formatted very easily.
Here is one possible solution to your problem:
$testDate = '2014-04-22';
$testTime = '21:02:17';
$newDateD = date('Y-m-d', strtotime($testDate));
$newDateT = date('H:i:s', strtotime($testTime));
$newBoth = date('Y-m-d H:i:s', strtotime($testDate .' ' .$testTime)); // works withour space!!!!
$Required = '2014-04-22 21:02:17';
echo '<br />$testDate: '. $testDate;
echo '<br />$testTime: '. $testTime;
echo '<br />';
echo '<br />$newDateD: '. $newDateD;
echo '<br />$newDateT: '. $newDateT;
echo '<br />$newBoth : '. $newBoth;
echo '<br />';
echo '<br />$Required: '. $Required;
echo '<br /><br />';
if($Required === $newBoth)
{
echo 'Matched OK ';
}else{
echo 'Failed';
}
echo '<br />' .$newBoth .' === '. $Required;
echo '<br /><br />';
Output
$testDate: 2014-04-22
$testTime: 21:02:17
$newDateD: 2014-04-22
$newDateT: 21:02:17
$newBoth : 2014-04-22 21:02:17
$Required: 2014-04-22 21:02:17
Matched OK
2014-04-22 21:02:17 === 2014-04-22 21:02:17