ILIAS  release_8 Revision v8.24
DateTimeTransformation.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22
27use DateTimeImmutable;
28use DateTimeInterface;
29
38{
41
45 public function transform($from): DateTimeImmutable
46 {
47 if ($from instanceof DateTimeImmutable) {
48 return $from;
49 }
50
51 $formats = [
52 DateTimeInterface::ATOM,
53 DateTimeInterface::COOKIE,
54 DateTimeInterface::ISO8601,
55 DateTimeInterface::RFC822,
56 DateTimeInterface::RFC7231,
57 DateTimeInterface::RFC3339_EXTENDED
58 ];
59
60 if (is_string($from)) {
61 foreach ($formats as $format) {
62 $res = DateTimeImmutable::createFromFormat($format, $from);
63 if ($res instanceof DateTimeImmutable) {
64 return $res;
65 }
66 }
67 }
68
69 if (is_int($from) || is_float($from)) {
70 return new DateTimeImmutable("@" . round($from));
71 }
72
74 sprintf('Value "%s" could not be transformed.', var_export($from, true)),
75 'no_valid_datetime',
76 $from
77 );
78 }
79}
Transform date format to DateTimeImmutable Please note:
$formats
Definition: date.php:77
A transformation is a function from one datatype to another.
$res
Definition: ltiservices.php:69
$format
Definition: metadata.php:235
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...