HTML_QuickForm and date/time discovery
| From: | Paul M Jones | Date: | Fri, 02 Apr 2004 16:23:38 +0000 |
| Subject: | HTML_QuickForm and date/time discovery | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-26965@lists.php.net to get a copy of this message | ||
Hi, all,
In HTML_QuickForm (wonderful package!) the date.php setValue() method is a bit limited. As it stands, you can pass either an array of date piece keys ('Y', 'm', 'd') and the corresponding values, or you can pass a Unix time stamp, and the setValue() method will properly set the "selected" attribute for the options. However, if you pass a regular date or time string to setValue(), the method won't set the options properly.
I have modified the setValue() method for date.php slightly so that you can pass a date or time string, and setValue() will convert it within the bounds of the Unix epoch. In short, the new method looks at the value, determines if it's a Unix timestamp, and attempts to convert it into a timestamp if it's not one already. Perhaps the leads might include this in the next release of HTML_QuickForm?
function setValue($value)
{
if ($this->_options['addEmptyOption'] && empty($value)) {
$value = array();
} else {
if (! is_array($value)) {
// unix timestamps fall in the range of -2^31
// to +2^31 (minus one); is $value outside of
// that range?
if (! is_numeric($value) ||
$value < (-1 * pow(2,31)) ||
$value > (pow(2,16) - 1)) {
// doesn't look like a unix timestamp,
// attempt to convert it.
$value = strtotime($value);
}
// now explode the unix timestamp
$arr = explode('-', date('w-d-n-Y-h-H-i-s-a-A', (int)$value));
$value = array(
'D' => $arr[0],
'l' => $arr[0],
'd' => $arr[1],
'M' => $arr[2],
'm' => $arr[2],
'F' => $arr[2],
'Y' => $arr[3],
'y' => $arr[3],
'h' => $arr[4],
'H' => $arr[5],
'i' => $arr[6],
's' => $arr[7],
'a' => $arr[8],
'A' => $arr[9]
);
}
}
parent::setValue($value);
}
--
Paul M. Jones
Savant: the simple alternative to Smarty.
http://phpsavant.com/