So I have included a date field in my custom block form and would like to use the date.

However it is saved as an array which I am finding a little difficult to work with:

The format for "#default_value" and "#return_value" is an array with three elements with the keys: 'year', month', and 'day'. For example, array('year' => 2007, 'month' => 2, 'day' => 15)

What is the best way of getting this "date array" into something I can use strtotime() on? Am I missing some Drupal way of allowing me to format this nicely into a string?

What I have working seems to be very long winded and static:

$startDate = variable_get('start_date', NULL);
if (isset($startDatea)) {
  $startDate = $startDatea['month'].'/'.$startDatea['day'].'/'.$startDatea['year'];
  $startDate = date('Y-m-d', strtotime($startDate));

I am sure there must be a nicer way.

1 Answer 1


For what it's worth you have a typo in your sample code, $startDate is being referenced as $startDatea - note the extra "a". As for a shorter version, try:

$startDate = variable_get('start_date', NULL);
$startDate = date('Y-m-d', mktime(0,0,0, $startDate['month'], $startDate['day'], $startDate['year']);

I suspect not jumping from numeric to string and then back again might save you a few cycles at the PHP layer.

Your Answer

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

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