Reputation: 762
Very stumped about this,
I have a text input field that allows the user to insert a date and time with jquery's date picker and a timepicker addon. When the user closes the calendar to insert the date, It enters this into the text field: 10/13/2013 22:16
. How do I convert this with php to a mysql timestamp function suitable for database storage? 2013-10-13 22:16:00
Thank You!
Upvotes: 1
Views: 1968
Reputation: 41
try the following in PHP:
$date= date('Y-m-d H:i:s',strtotime( $_POST['date']));
Upvotes: 1
Reputation: 103
If you are using jquery UI datepicker, then CIRCLE's answer is the right one. Changing the datepicker format to yy-mm-dd will turn 9/4/2014 into 2014-09-04 which can then be successfully compared against a timestamp stored in the database.
Upvotes: 0
Reputation: 13263
Make use of the builtin DateTime
class:
// Create a DateTime instance from a custom format
$format = 'm/d/Y H:i';
$date = '10/13/2013 22:16';
$time = DateTime::createFromFormat($format, $date);
// If the $time variable is false then it means
// that the input is not formatted correctly...
if ( ! $time) {
echo 'Input is not formatted correctly!';
} else {
echo 'Valid input: ', $time->format('r');
}
die;
Upvotes: 1
Reputation: 762
This is the only logical way I've figured out how to accomplish this with PHP.
$Data = $_POST['date'];
$Data = explode("/", $Data);
$Month = $Data[0];
$Day = $Data[1];
$YearAndTime = explode(" ", $exp[2]);
$Year = $YearAndTime[0];
$T = explode(":", $YearAndTime[1]);
$Hour = $T[0];
$Minute = $T[1];
$timestamp = "{$Year}-{$Month}-{$Day} {$Hour}:{$Minute}:00";
Upvotes: 2