/** * Add a selection drop down for the hour, minute and second to be selected. * @param string $name The name of the selection element * @param I2CE_Date $default The default I2CE_Date object to use to preset the value. * @param boolean $showError A flag if this field is currently invalid to mark it as such. * @param DOMNode $node The node to append the element to. * @param boolean $hidden Set to true if the form element should be hidden. */ public static function addTimeElement($template, $name, $default, $showError, $node, $hidden = false) { if ($hidden) { $hour = $template->createElement("input", array("name" => $name . "[hour]", "type" => "hidden", "value" => $default->hour())); $minute = $template->createElement("input", array("name" => $name . "[minute]", "type" => "hidden", "value" => $default->minute())); $second = $template->createElement("input", array("name" => $name . "[second]", "type" => "hidden", "value" => $default->second())); } else { $hour = $template->createElement("select", array("name" => $name . "[hour]", "class" => "date_hour" . ($showError ? "_error" : ""))); for ($i = 0; $i <= 23; $i++) { $opt = $template->createElement("option", array("value" => $i), $i); if ($i == $default->isHour($i)) { $opt->setAttribute("selected", "selected"); } $hour->appendChild($opt); } $minute = $template->createElement("select", array("name" => $name . "[minute]", "class" => "date_minute" . ($showError ? "_error" : ""))); for ($i = 0; $i <= 59; $i++) { $opt = $template->createElement("option", array("value" => $i), $i); if ($i == $default->isMinute($i)) { $opt->setAttribute("selected", "selected"); } $minute->appendChild($opt); } $second = $template->createElement("select", array("name" => $name . "[second]", "class" => "date_second" . ($showError ? "_error" : ""))); for ($i = 0; $i <= 59; $i++) { $opt = $template->createElement("option", array("value" => $i), $i); if ($i == $default->isSecond($i)) { $opt->setAttribute("selected", "selected"); } $second->appendChild($opt); } } $node->appendChild($hour); $node->appendChild($minute); $node->appendChild($second); }
/** * Compares a date to this one and returns -1 if it is before, 0 if the same and 1 if after * this date. * @param I2CE_Date $date * @return integer */ public function compare($date) { if (!$date instanceof I2CE_Date) { I2CE::raiseError("Invalid date comparison"); return null; } switch ($this->type) { case self::YEAR_ONLY: return bccomp($date->year(), $this->year); break; case self::YEAR_MONTH: $year_cmp = bccomp($date->year(), $this->year); if ($year_cmp == 0) { return bccomp($date->month(), $this->month); } else { return $year_cmp; } break; case self::MONTH_DAY: $month_cmp = bccomp($date->month(), $this->month); if ($month_cmp == 0) { return bccomp($date->day(), $this->day); } else { return $month_cmp; } break; case self::DATE: $year_cmp = bccomp($date->year(), $this->year); if ($year_cmp == 0) { $month_cmp = bccomp($date->month(), $this->month); if ($month_cmp == 0) { return bccomp($date->day(), $this->day); } else { return $month_cmp; } } else { return $year_cmp; } break; case self::DATE_TIME: $year_cmp = bccomp($date->year(), $this->year); if ($year_cmp == 0) { $month_cmp = bccomp($date->month(), $this->month); if ($month_cmp == 0) { $day_cmp = bccomp($date->day(), $this->day); if ($day_cmp == 0) { $hour_cmp = bccomp($date->hour(), $this->hour); if ($hour_cmp == 0) { $min_cmp = bccomp($date->minute(), $this->minute); if ($min_cmp == 0) { return bccomp($date->second, $this->second); } else { return $min_cmp; } } else { return $hour_cmp; } } else { return $day_cmp; } } else { return $month_cmp; } } else { return $year_cmp; } break; case self::TIME_ONLY: $hour_cmp = bccomp($date->hour(), $this->hour); if ($hour_cmp == 0) { $min_cmp = bccomp($date->minute(), $this->minute); if ($min_cmp == 0) { return bccomp($date->second, $this->second); } else { return $min_cmp; } } else { return $hour_cmp; } break; default: I2CE::raiseError("An invalid date type was used for I2CE_Date::compare.", E_USER_WARNING); return 0; break; } }