I have "2019-11-04 07:30:00+01" as datetime value. I am not getting how to handle the "+1" part in a Drupal 8 date field.

I am using this code, but it's not giving me any value.

use Drupal\Core\Datetime;

$userTimezone = new DateTimeZone(drupal_get_user_timezone());
$gmtTimezone = new DateTimeZone('GMT');
$myDateTime = new DateTime($item['start_date'], $gmtTimezone);

There is no DateTime class in Drupal\Core\Datetime you should use DrupalDateTime class instead try with.

    use Drupal\Core\Datetime\DrupalDateTime;
   //$userTimezone = new DateTimeZone(drupal_get_user_timezone());
    $gmtTimezone = new DateTimeZone('GMT');
    $myDateTime = new DrupalDateTime($item['start_date'], $gmtTimezone);
    $dateTime = $myDateTime->getPhpDateTime();
| improve this answer | |

There are three errors in that code:

  1. use requires the fully qualified name of a class, not its namespace.

  2. Drupal doesn't have any Drupal\Core\Datetime class. There is the Drupal\Core\Datetime\DrupalDateTime class, which is the class you want to use.

  3. The second argument of the constructor of DrupalDateTime isn't necessary since, as the documentation for DrupalDateTime::__construct() says, it's not used when the string passed as first argument specifies a timezone.

    mixed $timezone: PHP DateTimeZone object, string or NULL allowed. Defaults to NULL. Note that the $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00). @see http://php.net/manual/datetime.construct.php

The code you should use is then the following one.

use Drupal\Core\Datetime\DrupalDateTime;

$myDateTime = new DrupalDateTime($item['start_date']);

The code you are using doesn't return any value because you are using a class that doesn't exist. It should raise an error, as the code is referring a class that PHP cannot find.

As side note, the DrupalDateTime class should be used when a class, a method, or a function requires an instance of that class, you need a class that is more flexible than the DateTime class, or you need one of the methods that Drupal extends, for example DrupalDateTime::format().
In the other cases, you should use the DateTime class implemented in PHP.

See also the documentation for DateTimePlus, which makes the difference between the PHP class and the Drupal classes clearer.

This class wraps the PHP DateTime class with more flexible initialization parameters, allowing a date to be created from an existing date object, a timestamp, a string with an unknown format, a string with a known format, or an array of date parts. It also adds an errors array and a __toString() method to the date object.

This class is less lenient than the DateTime class. It changes the default behavior for handling date values like '2011-00-00'. The DateTime class would convert that value to '2010-11-30' and report a warning but not an error. This extension treats that as an error.

As with the DateTime class, a date object may be created even if it has errors. It has an errors array attached to it that explains what the errors are. This is less disruptive than allowing datetime exceptions to abort processing. The calling script can decide what to do about errors using hasErrors() and getErrors().

| improve this answer | |

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.