[Date class] Parsing of the input date in SetDate

From: Date: Thu, 06 Feb 2003 14:00:07 +0000
Subject: [Date class] Parsing of the input date in SetDate
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-13009@lists.php.net to get a copy of this message
Hi, I've found something weird in the Date.php class, perhaps can someone see if it's correct, or if I'm missing something ? Using the following code : include 'Date.php'; $dat = new Date(); $dat->setDate('0000-00-00 00:00:00', DATE_FORMAT_ISO); echo $dat->format('%m/%d/%Y'); "00/00/0" is returned instead of "00/00/0000" which should be the correct answer IMHO... The problem comes from the parsing of the date in the setDate method that converts the year '0000' into an unsigned int 0. This problem can be taken in consideration, for example with the use of a regular expression in setDate (You can find a patch attached) that keeps the correct input '0000' while checking if the date is well formed. Or perhaps does someone have a better idea ? Regards, -- Frederic Poeydomenge

diff Date.php newDate.php 137,138c137,151 < list($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second) = < sscanf($date, "%04u-%02u-%02u %02u:%02u:%02u"); --- > if (ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})",$date,$regs)) { > $this->year = $regs[1]; > $this->month = $regs[2]; > $this->day = $regs[3]; > $this->hour = $regs[4]; > $this->minute = $regs[5]; > $this->second = $regs[6]; > } else { > $this->year = 0; > $this->month = 1; > $this->day = 1; > $this->hour = 0; > $this->minute = 0; > $this->second = 0; > } 141,142c154,168 < list($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second) = < sscanf($date, "%04u%02u%02u%02u%02u%02u"); --- > if (ereg("([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})",$date,$regs)) { > $this->year = $regs[1]; > $this->month = $regs[2]; > $this->day = $regs[3]; > $this->hour = $regs[4]; > $this->minute = $regs[5]; > $this->second = $regs[6]; > } else { > $this->year = 0; > $this->month = 1; > $this->day = 1; > $this->hour = 0; > $this->minute = 0; > $this->second = 0; > }
« previous php.pear.dev (#13009) next »