Converting Form API date field to timestamp

If you are not using a date module and just want to use date field from form api, you will want to insert it into DB, probably as timestamp. So to convert that value to timestamp you need to use some precoding for that. In this example we also use a simple textfield to enter time value and add it to timestamp. Also take a note we are using timezone value.


$timezone = $user->timezone;
$dateFrom = $form_state['values']['schedule_container']['date_container']['from']['date'];
$submittedFrom = "{$dateFrom['year']}-{$dateFrom['month']}-{$dateFrom['day']}-{$form_state['values']['schedule_container']['date_container']['from']['time']}";
$newFrom = new DateTime($submittedFrom, new DateTimeZone($timezone));
$unixtsFrom = $newFrom->getTimestamp();

And then when you want to get date back from timestamp just use

format_date($unixtsFrom, $type = 'long', $format = '', $timezone )