note 96875 added to class.datetime

From: Date: Sat, 20 Mar 2010 09:21:05 +0000
Subject: note 96875 added to class.datetime
Groups: php.notes 
Request: Send a blank email to php-notes+get-167248@lists.php.net to get a copy of this message
If you need DateTime::createFromFormat functionality in versions <5.3.0 releases you might use the following basic class which can also be combined with Tom's class. It should work for most basic formats, however you should improve this function if you need more complex formats. <?php class DateClass extends DateTime{ public function getTimestamp(){ return $this->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<Y>19|20\d\d)"; $regexpArray['m'] = "(?P<m>0[1-9]|1[012])"; $regexpArray['d'] = "(?P<d>0[1-9]|[12][0-9]|3[01])"; $regexpArray['-'] = "[-]"; $regexpArray['.'] = "[\. /.]"; $regexpArray[':'] = "[:]"; $regexpArray['space'] = "[\s]"; $regexpArray['H'] = "(?P<H>0[0-9]|1[0-9]|2[0-3])"; $regexpArray['i'] = "(?P<i>[0-5][0-9])"; $regexpArray['s'] = "(?P<s>[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; } } ?> ---- Server IP: 85.214.154.209 Probable Submitter: 84.147.34.246 ---- Manual Page -- http://www.php.net/manual/en/class.datetime.php Edit -- https://master.php.net/note/edit/96875 Del: integrated -- https://master.php.net/note/delete/96875/integrated Del: useless -- https://master.php.net/note/delete/96875/useless Del: bad code -- https://master.php.net/note/delete/96875/bad+code Del: spam -- https://master.php.net/note/delete/96875/spam Del: non-english -- https://master.php.net/note/delete/96875/non-english Del: in docs -- https://master.php.net/note/delete/96875/in+docs Del: other reasons-- https://master.php.net/note/delete/96875 Reject -- https://master.php.net/note/reject/96875 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#167248) next »