DateTimeHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $text,
  34. $attr
  35. )) {
  36. $start = new \DateTime();
  37. $start->setDate($attr['y'], $attr['m'], $attr['d']);
  38. $start->setTime(
  39. isset($attr['h']) ? $attr['h'] : 0,
  40. isset($attr['i']) ? $attr['i'] : 0,
  41. isset($attr['s']) ? $attr['s'] : 0
  42. );
  43. if(isset($attr['h'])) {
  44. $interval = new \DateInterval('PT1S');
  45. } else {
  46. $interval = new \DateInterval('P1D');
  47. }
  48. $end = clone $start;
  49. $end->add($interval);
  50. return new \DatePeriod($start, $interval, $end);
  51. } else {
  52. throw new \InvalidArgumentException(
  53. sprintf("could not parse string '%s'", $text)
  54. );
  55. }
  56. }
  57. }
  58. /**
  59. * @param string|null $text
  60. * @return \DateTime|null
  61. */
  62. public static function parseGetStart($text)
  63. {
  64. $period = self::parse($text);
  65. if($period) {
  66. return $period->start;
  67. } else {
  68. return null;
  69. }
  70. }
  71. public static function deinvertInterval(\DateInterval $source = null)
  72. {
  73. // \DateInterval does not implement clone.
  74. // @see https://bugs.php.net/bug.php?id=50559
  75. $result = unserialize(serialize($source));
  76. if($result->invert) {
  77. $result->y *= -1;
  78. $result->m *= -1;
  79. $result->d *= -1;
  80. $result->h *= -1;
  81. $result->i *= -1;
  82. $result->s *= -1;
  83. $result->invert = 0;
  84. }
  85. return $result;
  86. }
  87. /**
  88. * @param \DateInterval|null $i
  89. * @return string|null
  90. */
  91. public static function intervalToIso(\DateInterval $i = null)
  92. {
  93. if(is_null($i)) {
  94. return null;
  95. } else {
  96. $i = self::deinvertInterval($i);
  97. if($i->y < 0 || $i->m < 0 || $i->d < 0 || $i->h < 0 || $i->i < 0 || $i->s < 0) {
  98. throw new \Exception('negative intervals are not supported');
  99. } else {
  100. return $i->format('P%yY%mM%dDT%hH%iM%sS');
  101. }
  102. }
  103. }
  104. }