diff --git a/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php b/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php index 8d47598755..bffbbfd8d2 100644 --- a/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php +++ b/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php @@ -26,15 +26,18 @@ final class ODM extends BaseAdapterODM implements SoftDeleteableAdapter */ public function getDateValue($meta, $field) { + $datetime = new \DateTime(); $mapping = $meta->getFieldMapping($field); - if (isset($mapping['type']) && 'timestamp' === $mapping['type']) { - return time(); + $type = $mapping['type'] ?? null; + + if ('timestamp' === $type) { + return (int) $datetime->format('U'); } - if (isset($mapping['type']) && in_array($mapping['type'], ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) { - return new \DateTimeImmutable(); + + if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) { + return \DateTimeImmutable::createFromMutable($datetime); } - return \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', '')) - ->setTimeZone(new \DateTimeZone(date_default_timezone_get())); + return $datetime; } } diff --git a/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php b/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php index 2b4744a096..6c28005890 100644 --- a/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php +++ b/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php @@ -44,15 +44,17 @@ public function getDateValue($meta, $field) */ private function getRawDateValue(array $mapping) { - if (isset($mapping['type']) && 'integer' === $mapping['type']) { - return time(); + $datetime = new \DateTime(); + $type = $mapping['type'] ?? null; + + if ('integer' === $type) { + return (int) $datetime->format('U'); } - if (isset($mapping['type']) && in_array($mapping['type'], ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) { - return new \DateTimeImmutable(); + if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) { + return \DateTimeImmutable::createFromMutable($datetime); } - return \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', '')) - ->setTimeZone(new \DateTimeZone(date_default_timezone_get())); + return $datetime; } } diff --git a/src/Timestampable/Mapping/Event/Adapter/ODM.php b/src/Timestampable/Mapping/Event/Adapter/ODM.php index 41c0f48750..5b35b68f17 100644 --- a/src/Timestampable/Mapping/Event/Adapter/ODM.php +++ b/src/Timestampable/Mapping/Event/Adapter/ODM.php @@ -26,15 +26,18 @@ final class ODM extends BaseAdapterODM implements TimestampableAdapter */ public function getDateValue($meta, $field) { + $datetime = new \DateTime(); $mapping = $meta->getFieldMapping($field); - if (isset($mapping['type']) && 'timestamp' === $mapping['type']) { - return time(); + $type = $mapping['type'] ?? null; + + if ('timestamp' === $type) { + return (int) $datetime->format('U'); } - if (isset($mapping['type']) && in_array($mapping['type'], ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) { - return new \DateTimeImmutable(); + + if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) { + return \DateTimeImmutable::createFromMutable($datetime); } - return \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', '')) - ->setTimeZone(new \DateTimeZone(date_default_timezone_get())); + return $datetime; } } diff --git a/src/Timestampable/Mapping/Event/Adapter/ORM.php b/src/Timestampable/Mapping/Event/Adapter/ORM.php index 4399486c97..9ba94437e4 100644 --- a/src/Timestampable/Mapping/Event/Adapter/ORM.php +++ b/src/Timestampable/Mapping/Event/Adapter/ORM.php @@ -44,15 +44,17 @@ public function getDateValue($meta, $field) */ private function getRawDateValue(array $mapping) { - if (isset($mapping['type']) && 'integer' === $mapping['type']) { - return time(); + $datetime = new \DateTime(); + $type = $mapping['type'] ?? null; + + if ('integer' === $type) { + return (int) $datetime->format('U'); } - if (isset($mapping['type']) && in_array($mapping['type'], ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) { - return new \DateTimeImmutable(); + if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) { + return \DateTimeImmutable::createFromMutable($datetime); } - return \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', '')) - ->setTimeZone(new \DateTimeZone(date_default_timezone_get())); + return $datetime; } }