DateTimeHelper.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace fphammerle\helpers;
  3. class DateTimeHelper
  4. {
  5. /**
  6. * @param integer|null $timestamp unix timestamp
  7. * @return \DateTime|null
  8. */
  9. public static function timestampToDateTime($timestamp)
  10. {
  11. if($timestamp === null) {
  12. return null;
  13. } elseif(is_int($timestamp)) {
  14. $dt = new \DateTime();
  15. $dt->setTimestamp($timestamp);
  16. return $dt;
  17. } else {
  18. throw new \InvalidArgumentException('expected integer or null');
  19. }
  20. }
  21. /**
  22. * @param string|null $text
  23. * @return \DatePeriod|null
  24. */
  25. public static function parse($text)
  26. {
  27. if($text === null) {
  28. return null;
  29. } else {
  30. if(preg_match(
  31. '/^(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})'
  32. .'([ T](?P<h>\d{2}):(?P<i>\d{2}):(?P<s>\d{2}))?'
  33. . '(Z|[\+-]\d{2}:\d{2})?$/',
  34. $text,
  35. $attr
  36. )) {
  37. $start = new \DateTime($attr[0]);
  38. $start->setDate($attr['y'], $attr['m'], $attr['d']);
  39. $start->setTime(
  40. empty($attr['h']) ? 0 : $attr['h'],
  41. empty($attr['i']) ? 0 : $attr['i'],
  42. empty($attr['s']) ? 0 : $attr['s']
  43. );
  44. if(!empty($attr['h'])) {
  45. $interval = new \DateInterval('PT1S');
  46. } else {
  47. $interval = new \DateInterval('P1D');
  48. }
  49. $end = clone $start;
  50. $end->add($interval);
  51. return new \DatePeriod($start, $interval, $end);
  52. } else {
  53. throw new \InvalidArgumentException(
  54. sprintf("could not parse string '%s'", $text)
  55. );
  56. }
  57. }
  58. }
  59. /**
  60. * @param string|null $text
  61. * @return \DateTime|null
  62. */
  63. public static function parseGetStart($text)
  64. {
  65. $period = self::parse($text);
  66. if($period) {
  67. return $period->start;
  68. } else {
  69. return null;
  70. }
  71. }
  72. public static function deinvertInterval(\DateInterval $source = null)
  73. {
  74. // \DateInterval does not implement clone.
  75. // @see https://bugs.php.net/bug.php?id=50559
  76. $result = unserialize(serialize($source));
  77. if($result->invert) {
  78. $result->y *= -1;
  79. $result->m *= -1;
  80. $result->d *= -1;
  81. $result->h *= -1;
  82. $result->i *= -1;
  83. $result->s *= -1;
  84. $result->invert = 0;
  85. }
  86. return $result;
  87. }
  88. /**
  89. * @param \DateInterval|null $i
  90. * @return string|null
  91. */
  92. public static function intervalToIso(\DateInterval $i = null)
  93. {
  94. if(is_null($i)) {
  95. return null;
  96. } else {
  97. $i = self::deinvertInterval($i);
  98. if($i->y < 0 || $i->m < 0 || $i->d < 0 || $i->h < 0 || $i->i < 0 || $i->s < 0) {
  99. throw new \Exception('negative intervals are not supported');
  100. } else {
  101. return $i->format('P%yY%mM%dDT%hH%iM%sS');
  102. }
  103. }
  104. }
  105. /**
  106. * @param \DatePeriod|null $p
  107. * @return string|null
  108. */
  109. public static function periodToIso(\DatePeriod $p = null)
  110. {
  111. if(is_null($p)) {
  112. return null;
  113. } else {
  114. // Cave:
  115. // (new \DatePeriod(
  116. // new \DateTime('2016-08-05T14:50:14Z'),
  117. // new \DateInterval('P1D'),
  118. // -1
  119. // )->recurrences == 1
  120. if($p->recurrences <= 0) {
  121. throw new \Exception(
  122. 'conversion of periods with number of occurances'
  123. . ' being negative is not supported'
  124. );
  125. }
  126. $repetitions = -1;
  127. foreach($p as $dt) {
  128. $repetitions++;
  129. // printf("%d. %s\n", $repetitions, $dt->format(\DateTime::ATOM));
  130. }
  131. switch($repetitions) {
  132. case -1:
  133. // no valid date within period
  134. // e.g. new \DatePeriod(
  135. // new \DateTime('2016-08-05T14:50:14+08:00'),
  136. // new \DateInterval('PT1S'),
  137. // new \DateTime('2016-08-05T14:50:14+08:00')
  138. // )
  139. throw new \InvalidArgumentException(
  140. 'given period does not contain any valid date'
  141. );
  142. case 0:
  143. return sprintf(
  144. '%s/%s',
  145. $p->getStartDate()->format(\DateTime::ATOM),
  146. self::intervalToIso($p->getDateInterval())
  147. );
  148. default:
  149. return sprintf(
  150. 'R%d/%s/%s',
  151. $repetitions,
  152. $p->getStartDate()->format(\DateTime::ATOM),
  153. self::intervalToIso($p->getDateInterval())
  154. );
  155. }
  156. }
  157. }
  158. }