I have two datetime fields, one only accepts date input and the other is only accepting time input (via the technique where you set the ['#date_date_element'] = 'none' as described in this answer). In hook_node_presave the date field shows a value like 2018-10-11 and the time field shows a value like 2018-10-11T23:36:01-0400. I'm altering the latter string - I'm changing just the date portion to match the value from my date only field. The problem I'm running into is that, for some reason, every time I save the node the time field goes back an hour, e.g. 10:00:00 to 09:00:00 to 08:00:00. This does not happen when I comment out my code in presave AND it also does not happen on my local machine, only on the remote development environment.

function MYMODULE_node_presave($node) {
  $time = $node->field_time->value;
  $node->field_time->value = $node->field_date->value . substr($time, strpos($time, 'T'));

What can be causing this behavior?

(FYI: I'm abandoning this approach and I'm going to use the time_field module)

  • Did you check the time zone set in the remote server timedatectl | grep “Time zone”?
    – berramou
    Commented Oct 12, 2018 at 22:06
  • Time zone: Etc/UTC (UTC, +0000). Drupal is set to America/New_York
    – sonfd
    Commented Oct 12, 2018 at 22:57
  • Try to set drupal timezone to america/New_York date_default_timezone_set('America/New_York'); and test if the issue comes from time zone .
    – berramou
    Commented Oct 12, 2018 at 23:02
  • 1
    Can you confirm the field types and config of field_time and field_date? Storage of datetime fields is UTC w/o an offset string. I am also a little confused that you are saying that field_date is date-only but showing '2018-10-11T23:36:01-0400' since if it should really just be a Y-m-d string as the value, and if you are working with the date object, the time portion should be noon UTC.
    – mpdonadio
    Commented Oct 13, 2018 at 16:18
  • Yes, you're right about the date field's value, sorry I had that wrong in the description. The fields are both datetime fields.
    – sonfd
    Commented Oct 13, 2018 at 18:13


Your Answer

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

Browse other questions tagged or ask your own question.