0

How can i save date array to database?

the code belove creates a form for date

$form['date_time'] = array(
    '#title' => t('Date'),
    '#type' => 'date',
    '#description' => t('Please enter the date.'),

);

User then selects the month,day,year.

How can i convert it so that i can save it to database?

function repman_form_submit($form, &$form_state){


drupal_set_message($form_state['values']['date_time']);

I am just using the drupal_set_message to view the output. And i get array.

UPDATE:

Done it using this

            'date_month'=> $form_state['values']['date_time']['month'],
    'date_day' => $form_state['values']['date_time']['day'],
    'date_year'=> $form_state['values']['date_time']['year'],

1 Answer 1

1

You want to convert the date to ATOM or a unix timestamp:

// Dates are stored in ATOM format. 1969-12-31T19:33:33-05:00
$value = date(DATE_ATOM, mktime(0, 0, 0, $m, $d, $y));

// Convert to unix time
$value = date('U', mktime(0, 0, 0, $m, $d, $y));

I believe sql datetime uses ATOM but you can also stor it as a unix time stamp if you want to just stor it as an integer value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, i just broke it into 3 peaces then saved them into the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.