ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCmiXapiDateTime.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31{
32 // DateTime::RFC3339_EXTENDED resolves to Y-m-d\TH:i:s.vP
33 // note the v at the end -> this works with PHP 7.3
34 // but not with PHP 7.2, 7.1 and probably not with versions below
35
36 public const RFC3336_EXTENDED_FIXED_USING_u_INSTEAD_OF_v = 'Y-m-d\TH:i:s.uP';
37
38 public function toXapiTimestamp(): string
39 {
40 $phpDateTime = new DateTime();
41 $phpDateTime->setTimestamp((int) $this->get(IL_CAL_UNIX));
42
43 return $phpDateTime->format(self::RFC3336_EXTENDED_FIXED_USING_u_INSTEAD_OF_v);
44 }
45
49 public static function fromXapiTimestamp(string $xapiTimestamp): \ilCmiXapiDateTime
50 {
51 $phpDateTime = DateTime::createFromFormat(
52 self::RFC3336_EXTENDED_FIXED_USING_u_INSTEAD_OF_v,
53 $xapiTimestamp
54 );
55
56 $unixTimestamp = $phpDateTime->getTimestamp();
57
58 return new self($unixTimestamp, IL_CAL_UNIX);
59 }
60
64 public static function fromIliasDateTime(ilDateTime $dateTime): \ilCmiXapiDateTime
65 {
66 return new self($dateTime->get(IL_CAL_UNIX), IL_CAL_UNIX);
67 }
68
69 public static function dateIntervalToISO860Duration(\DateInterval $d): string
70 {
71 $duration = 'P';
72 if (!empty($d->y)) {
73 $duration .= "{$d->y}Y";
74 }
75 if (!empty($d->m)) {
76 $duration .= "{$d->m}M";
77 }
78 if (!empty($d->d)) {
79 $duration .= "{$d->d}D";
80 }
81 if (!empty($d->h) || !empty($d->i) || !empty($d->s)) {
82 $duration .= 'T';
83 if (!empty($d->h)) {
84 $duration .= "{$d->h}H";
85 }
86 if (!empty($d->i)) {
87 $duration .= "{$d->i}M";
88 }
89 if (!empty($d->s)) {
90 $duration .= "{$d->s}S";
91 }
92 // ToDo: nervt!
93 /*
94 if (!empty($d->f)) {
95 if (!empty($d->s)) {
96 $s = $d->s + $d->f;
97 }
98 else {
99 $s = $d->f;
100 }
101 $duration .= "{$s}S";
102 }
103 else
104 {
105 if (!empty($d->s)) {
106 $duration .= "S";
107 }
108 }
109 */
110 }
111 if ($duration === 'P') {
112 $duration = 'PT0S'; // Empty duration (zero seconds)
113 }
114 return $duration;
115 }
116}
$duration
const IL_CAL_UNIX
static fromXapiTimestamp(string $xapiTimestamp)
static fromIliasDateTime(ilDateTime $dateTime)
const RFC3336_EXTENDED_FIXED_USING_u_INSTEAD_OF_v
static dateIntervalToISO860Duration(\DateInterval $d)
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date