format ("U"); } /** * This function calculates the number of days between the first and the second date. Arguments must be subclasses of DateTime **/ static function differenceInDays ($firstDate, $secondDate){ $firstDateTimeStamp = $firstDate->format("U"); $secondDateTimeStamp = $secondDate->format("U"); $rv = round ((($firstDateTimeStamp - $secondDateTimeStamp))/86400); return $rv; } /** * This function returns an object of DateClass from $time in format $format. See date() for possible values for $format **/ static function createFromFormat ($format, $time){ assert ($format!=""); if($time==""){ return new DateClass(); } $regexpArray['Y'] = "(?P[12][90]\d\d)"; $regexpArray['m'] = "(?P0[1-9]|1[012])"; $regexpArray['d'] = "(?P0[1-9]|[12][0-9]|3[01])"; $regexpArray['-'] = "[-]"; $regexpArray['.'] = "[\. /.]"; $regexpArray[':'] = "[:]"; $regexpArray['/'] = "[\/]"; $regexpArray['space'] = "[\s]"; $regexpArray['H'] = "(?P0[0-9]|1[0-9]|2[0-3])"; $regexpArray['i'] = "(?P[0-5][0-9])"; $regexpArray['s'] = "(?P[0-5][0-9])"; $formatArray = str_split ($format); $regex = ""; // create the regular expression foreach($formatArray as $character){ if ($character==" ") $regex = $regex.$regexpArray['space']; elseif (array_key_exists($character, $regexpArray)) $regex = $regex.$regexpArray[$character]; } $regex = "/".$regex."/"; // get results for regualar expression preg_match ($regex, $time, $result); // create the init string for the new DateTime $initString = $result['Y']."-".$result['m']."-".$result['d']; // if no value for hours, minutes and seconds was found add 00:00:00 if (isset($result['H'])) $initString = $initString." ".$result['H'].":".$result['i'].":".$result['s']; else {$initString = $initString." 00:00:00";} $newDate = new DateClass ($initString); return $newDate; } } ?>