1

I'm trying to migrate some data from MySQL DateTime format. Here's one:

$old_date = "2019-05-06 08:00:00" My content type has a custom field using DateTime.

Using the info from SO links below, I can do this:

$date_object = DateTime::createFromFormat("Y-m-d H:i:s", $old_date);

which gives me a lovely object in my current timezone.

date: 2019-05-06 09:00:00.0 US/Central (-05:00)

MySQL is looking for a string in the UTC format. When I attempt to format this new object, it dutifully returns

$date_object->format(DATETIME_DATETIME_STORAGE_FORMAT); "2019-05-06T08:00:00"

But this is not helpful. Using the Drupal 8.7 admin interface, when I enter the date and time values, I see in MySQL table when I save the node, that I get:

2019-06-06T03:00:00

So, apparently Drupal Entity has a handler to automatically instantiate a localized DateTime value in MySQL that is UTC friendly. I ask because I haven't found what this method might be and I have not found it in DrupalDateTime or related classes. Happy to be shown what I've overlooked. Or how to discern how Entity handles all of these functions that I can use in my custom modules.

Or should I do the math for each of these strings and hope I get the correct timezone differential depending on the time of the year?

How to get formatted date string from a DateTimeItem object

Inserting the value from Datetime field form

  • 1
    Before you format in DATETIME_DATETIME_STORAGE_FORMAT you need to set the timezone DATETIME_STORAGE_TIMEZONE, or use the new interface, as explained in the answer to the question you've already linked drupal.stackexchange.com/a/204107/47547 – 4k4 Jul 23 '19 at 19:56
  • How are you doing your migration? The core Migrate module? The contrib Feeds module? Something custom? – mpdonadio Jul 23 '19 at 21:59
  • Because of the number of records, opted to do this custom. – sam452 Jul 24 '19 at 15:31
  • Excellent, @4k4 the new date object must be set to UTC which is what I missed. If you will post it as an answer I will accept. – sam452 Jul 24 '19 at 15:33
  • OK, I mark it as duplicate so that others can find the correct answer through your high quality question. – 4k4 Jul 24 '19 at 15:47

Browse other questions tagged or ask your own question.