i have a strange issue with a field "date". First i save it programatically this way :

$now = new \DateTime();
$node->set('field_paiement_date' => DrupalDateTime::createFromTimestamp($now->getTimestamp());

Then when i come on the content type in the back office i've got this value :enter image description here

And finally when i dump the field in question i got this value : enter image description here

My current timezone is 'Pacific/Noumea', so the correct value i want to show to the users is the one when i dump the field. What is the best way to do that ?


  • 2
    You've stored the time in your local time, but you need to store it in UTC, see drupal.stackexchange.com/a/246668
    – 4k4
    May 2 '18 at 6:26
  • Okay now it's works thank you ! I have an other issue, in my twig template when i want to use the date filter, it always print as UTC but not at my current timezone ...
    – lchabrand
    May 3 '18 at 0:02
  • 1
    ... then you print the raw database value. If you output the date field normally, that is by a field formatter configured in the display mode of the content type, then it should print the date in the current time zone. If you need to print the date field directly in twig, then use the date property, which is a formattable date object instead of the value property which is the raw data.
    – 4k4
    May 3 '18 at 6:40

Datetime values in storage are UTC, if you are storing datetime values by your own, you have to pay attention on this and display datetimes to users considering your timezone ('Pacific/Noumea' probably declared in your php.ini or user's timezone present in user profile).

Storing 2018-05-02T08:23:25 as your current datetime in caledonia in database is a nonsense considering timezone, because the format DateTimeItemInterface::DATETIME_STORAGE_FORMAT do not include timezone.

$date = strtotime($node->field_paiement_date->value);
  ->format($date, 'custom', 'Y-m-d H:i:s', drupal_get_user_timezone());
  ->format($date, 'custom', 'Y-m-d H:i:s', 'Pacific/Noumea');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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