Problem
Trying to create a DrupalDateTime object from timestamp : 1634590800 (2021-10-18 23:00:00 for my timezone).
The datetime objects returns 0800-10-18 16:34:59.0 Europe/Paris (+00:09)
This should work with timestamp according to the PHP Doc of
DrupalDateTime::__construct() method
.
=> It's working when the timestamp is converted to a datetime before :
$date = date('Y-m-d H:i:s', $timestamp);
$datetme = new Drupal\Core\Datetime\DrupalDateTime\DrupalDateTime($date);Steps to reproduce
$date = new Drupal\Core\Datetime\DrupalDateTime\DrupalDateTime($timestamp);
Proposed resolution
Use
Drupal\Component\Datetime\DateTimePlus::prepareFormat
by default to match the PHP Doc?
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | drupal-date-time-not-supporting-timestamps-3244294-5.patch | 561 bytes | murilohp |
Comments
Comment #2
cilefen commentedComment #3
murilohp commentedComment #5
murilohp commentedHey @pauline-g sorry for my delay for this response, I've assigned this to me and took a while to work on that, but I was able to reproduce this issue, and your idea solved the problem for me. The class DateTimePlus has the method prepareTime, and on it's doc says:
But the function is not preparing the string when it's a timestamp. So following your suggestion, the patch will validate and convert the $time to a readable date.
This problem was actually being caused by the function date_parse, used to convert date inside the __constructor, and this function only accepts date/time accepted by the DateTimeImmutable::__construct()., and timestamp is not valid for this scenario.
It would be nice to have more opinions regarding this scenario.
Thanks!
Comment #6
murilohp commentedComment #7
mfbThe documentation states
i.e. you need to prepend the numeric timestamp with
'@':which gives you
2021-10-18 21:00:00 +00:00