setTimestamp($timestamp); return $dt; } else { throw new \InvalidArgumentException('expected integer or null'); } } /** * @param string|null $text * @return DatePeriod|null */ public static function parse($text) { if($text === null) { return null; } else { if(preg_match( '/^(?P\d{4})-(?P\d{2})-(?P\d{2})' .'([ T](?P\d{2}):(?P\d{2}):(?P\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) ); } } } }