Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a form made with the Drupal Form API, there is a date field which result I want to output to the user in an email.

I'm only getting array as output, so how do I access the keys and values?

This is my code:

$mailText .= 'Selected date: ' . preg_replace("/(\n|\r)/s", '', $form_state['return_value']['myform_date']);
share|improve this question
    
I'm not sure I understand the question...what's in the array? If it's $x = array('foo' => 'bar'), for example, you can get the value of foo with $x['bar'] –  Clive Feb 9 at 16:07
    
array('year' => 2007, 'month' => 2, 'day' => 15) –  Johan Dahl Feb 9 at 16:12
    
And what problem are you having specifically when trying to access members of the array? –  Clive Feb 9 at 16:42

1 Answer 1

You can put your values through some date formatting using format_date(). You can change 'short' to another date type you have in Drupal such as 'medium' or 'long.'

$day = $form_state['return_value']['myform_date']['day'];
$month = $form_state['return_value']['myform_date']['month'];
$year = $form_state['return_value']['myform_date']['year'];
$timestamp = strtotime($month . '/' . $day . '/' . $year), 
$date = format_date($timestamp, 'short');
$mailText .= 'Selected date: ' . $date;

Alternatively you can use a custom date format

$date = format_date($timestamp, 'custom', 'F j, Y');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.