Using the Form API and hook_form_alter(), how can I manually set (force) the values for each of Drupal 7's date field types using a valid Unix Timestamp?

When the form is finally submit the field_date types are always empty arrays. Below is the broken code I'm working from.

hook_form_alter(&$form, &$form_state, $form_id) {

    // http://drupal.org/node/1455576 - Date
    $form['field_date'][LANGUAGE_NONE][0]['value'] = time();

    // http://drupal.org/node/1455576 - Date (ISO Format)
    $form['field_date_iso_format'][LANGUAGE_NONE][0]['value'] = time();

    // http://drupal.org/node/1455576 - Date (Unix Timestamp)
    $form['field_date_unix_timestamp'][LANGUAGE_NONE][0]['value'] =time();

I should mention the fields widget is set to text and it's format is 'Y-m-d H:i:s'

I have also tried date('Y-m-d H:i:s', time()) instead of time().

Example dpm() output.

Example dpm() output

I should also note that as a workaround I can set the field if I modify it in hook_node_presave(). To do this I set $node->field_date['und'][0]['value'] = date('Y-m-d H:i:s', $my_unix_timestamp); I would still like to understand how to do this using hook_form_alter instead.

  • In what way is this code broken ?
    – Countzero
    Commented Mar 30, 2012 at 5:34
  • both date('Y-m-d', strtotime('31 May 2011')) and time() are always empty. I'm starting to think I'm using the wrong array for dates. $form['date_field']['und'][0]['value'] may not be correct? Back to the docs it is.
    – Citricguy
    Commented Mar 30, 2012 at 7:54
  • When the form is finally submit the field_date types are always empty arrays.
    – Citricguy
    Commented Mar 30, 2012 at 8:01
  • Did you try to set $form['field_date'][LANGUAGE_NONE][0]['#default_value']['value'] ?
    – Countzero
    Commented Mar 30, 2012 at 8:10
  • Could you not try remove any custom code and submit the form from the ui to check the format saved natively from the field?
    – WestieUK
    Commented Mar 30, 2012 at 8:17

11 Answers 11


Here is a code that treat site timezone right for me:

 $node->field_date[LANGUAGE_NONE][0] = array(
    'value' => format_date(strtotime('now'), 'custom', 'Y-m-d H:i:s', 'UTC'),
    'timezone' => 'UTC',  
    'timezone_db' => 'UTC',
  • 1
    5 years later this is still a good answer. Wouldn't it make sense though to use 'timezone' => variable_get('date_default_timezone', 'UTC'), ? Commented Jun 27, 2017 at 7:10

Each date field-type is expecting a differently formatted timestamp (based on how it's stored in the DataBase):

// For datetime
$node->field_datetest[$node->language][0]['value'] = "2011-05-25 10:35:58";

// For date
$node->field_datetest[$node->language][0]['value'] = "2011-05-25T10:35:58";

// For datestamp
$node->field_datetest[$node->language][0]['value'] = strtotime("2011-05-25 10:35:58");

Note that you don't need to specify a complete date; for datetime and date you can just pad with zeros, e.g. "2011-05-00 00:00:00" (datetime), "2011-00-00T00:00:00" (date), etc. For datestamp you could just do e.g. strtotime("2011-05-25").

Important: Also note that while the exact value you specify will be stored in the database, the actual time displayed on the site might be different depending on timezone settings. When you create a new datetime/date/datestamp field, you get to choose between five different timezone handling methods. The default one is "site's time zone":

When entering data into the field, the data entered is assumed to be in the site's time zone. When the data is saved to the database, it is converted to UTC. However, if you set a date field programmatically like in the above example then no conversion takes place, so make sure you account for the field's timezone settings. Or in other words, if you use "site's time zone", make sure the time is in UTC.

This information was referenced from the section labeled Date field (datetime, date, datestamp) on fooninja's blog.

This is also a great general resource for programmatic node creation in Drupal 7.

  • You need quotes around the value keys Commented May 28, 2013 at 0:15
  • Thanks masterchief! Updated to reflect your improvement.
    – MD3
    Commented Aug 1, 2013 at 18:26

This worked for me.

$node->field_date[LANGUAGE_NONE][0] = array(
      'value' => date('Y-m-d H:i:s', strtotime('now')),
      'timezone' => 'UTC',
      'timezone_db' => 'UTC',

The time() value will not work at this level.

You have to use something like :

$mydate = date('Y-m-d', strtotime('31 May 2011'));
  • This works for me at hook_node_presave, but not in hook_form_alter.
    – Citricguy
    Commented Mar 30, 2012 at 9:59
  • Did you try to set $form['field_date'][LANGUAGE_NONE][0]['#default_value']['value'] as mentioned above ?
    – Countzero
    Commented Mar 30, 2012 at 10:16
  • Found it node presave at $node->field_date[LANGUAGE_NONE][0]
    – Citricguy
    Commented Jun 8, 2012 at 13:07
  • Not working with hook_form_alter... any help? Commented Jun 16, 2018 at 20:02

For Date stuff we could write a js for getting the current date...

jQuery(document).ready(function() {

    var fullDate = new Date()
    var HR_fullDate = fullDate.toDateString().toString().substring(4,15);


If you're using field type Date (ISO format) then use:

$node->field_datetest[$node->language][0][value] = "2011-05-25T10:35";

Try this:

$now = date_now('UTC');
$now = date_format($now, "Y-m-d H:i:s");

For those dealing with $form_state and popup widget:

// The date format in this sample is dd/mm/yyyy
$form_state['values']['field_my_date_field']['und'][0]['value']['date'] = '14/05/2014';

To set a value to a date field follow below codes.

Using hook_form_alter

$form["field_date"]["und"][0]['#default_value']['value'] = $new_date;

Using hook_node_submit or hook_node_presave (Saving the value to the database)

 function MY_MODULE_node_presave($node) 
      $node->field_date['und'][0]['value'] =  '2014-09-23 00:00:00';

Or if you need to save value of one custom field(defined in hook_form_alter) to another, see below codes.

function MY_MODULE_node_submit($node, $form, &$form_state) {
  if($node->type == 'my_node_type') {
    $test_date =  $form['test_date ']['#value'];
    //Ensure that $test_date format is '2014-09-23 00:00:00'
    $node->field_date['und'][0]['value'] =  $test_date ;

The accepted answer was giving me a PDO Exception. Only a simple time() call worked for a "datestamp" field!

        $entity->field_upload_time["und"][0]["value"] = time();
  • This was the only thing that worked for me as well. Commented Oct 2, 2020 at 16:03

used time() and worked OK....


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.