123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace fphammerle\helpers;
- class DateTimeHelper
- {
-
- public static function timestampToDateTime($timestamp)
- {
- if($timestamp === null) {
- return null;
- } elseif(is_int($timestamp)) {
- $dt = new \DateTime();
- $dt->setTimestamp($timestamp);
- return $dt;
- } else {
- throw new \InvalidArgumentException('expected integer or null');
- }
- }
-
- public static function parse($text)
- {
- if($text === null) {
- return null;
- } else {
- if(preg_match(
- '/^(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})'
- .'([ T](?P<h>\d{2}):(?P<i>\d{2}):(?P<s>\d{2}))?$/',
- $text,
- $attr
- )) {
- $start = new \DateTime();
- $start->setDate($attr['y'], $attr['m'], $attr['d']);
- $start->setTime(
- isset($attr['h']) ? $attr['h'] : 0,
- isset($attr['i']) ? $attr['i'] : 0,
- isset($attr['s']) ? $attr['s'] : 0
- );
- if(isset($attr['h'])) {
- $interval = new \DateInterval('PT1S');
- } else {
- $interval = new \DateInterval('P1D');
- }
- $end = clone $start;
- $end->add($interval);
- return new \DatePeriod($start, $interval, $end);
- } else {
- throw new \InvalidArgumentException(
- sprintf("could not parse string '%s'", $text)
- );
- }
- }
- }
-
- public static function parseGetStart($text)
- {
- $period = self::parse($text);
- if($period) {
- return $period->start;
- } else {
- return null;
- }
- }
- }
|