- $past = 60;
- $future = 60;
- echo "<select name=\"$name\">\n";
- $now = date('Y-m-d', time());
- list($y, $m, $d) = explode('-', $now);
- $today = mktime(0, 0, 0, $m, $d, $y);
- if (isset($date)) {
- list($y, $m, $d) = explode('-', $date);
- $then = mktime(0, 0, 0, $m, $d, $y);
- if ($then < $today - 86400 * $past || $then > $today + 86400 * $future) {
- option($name, $date, date('l j F Y', $then), $date);
+ if (! isset($date)) $date = date('Y-m-d', time());
+ datepicker($name, $date);
+ }
+
+ function validate_postcode($postcode, &$outward = null, &$inward = null) {
+ /*
+ Valid postcode formats (BS7666):
+
+ AN NLL
+ ABN NLL
+ ANN NLL
+ ABNN NLL
+ ABND NLL
+ ANC NLL
+
+ Where N is a number; A is a letter not including Q, V, X;
+ B is a letter not including I, J, Z; C is a letter from the set
+ ABCDEFGHJKSTUW; D is a letter from the set ABEHMNPRVWXY;
+ L is a letter from the set ABDEFGHJLNPQRSTUWXYZ.
+
+ The postcode GIR 0AA is also valid.
+ */
+ $outward = $inward = null;
+
+ /* Treat blank as valid for convenience. */
+ $postcode = trim($postcode);
+ if (! $postcode) return true;
+
+ $A = '[ABCDEFGHIJKLMNOPRSTUWYZ]';
+ $B = '[ABCDEFGHKLMNOPQRSTUVWXY]';
+ $C = '[ABCDEFGHJKSTUW]';
+ $D = '[ABEHMNPRVWXY]';
+ $L = '[ABDEFGHJLNPQRSTUWXYZ]';
+ $N = '\d';
+ if (! preg_match("/^($A$N|$A$B$N|$A$N$N|$A$B$N$N|$A$B$N$D|$A$N$C|GIR)\s*($N$L$L)$/", $postcode, $m)) return false;
+ if ($m[1] == "GIR" && $m[2] != "0AA") return false;
+ list($ignored, $outward, $inward) = $m;
+ return true;
+ }
+
+ function format_postcode($postcode, $complain = true) {
+ if (validate_postcode($postcode, $outward, $inward)) {
+ return "$outward $inward";
+ }
+ if ($complain) {
+ echo "<p>Invalid postcode!</p>\n";
+ return null;
+ }
+ }
+
+ function get_small_link_with_id() {
+ /* Args are <id>, <alt text>, <format>, [<stuff> ...] */
+ $args = func_get_args();
+ $id = array_shift($args);
+ if (isset($id)) $id = " id=\"$id\"";
+ $html = htmlspecialchars(array_shift($args));
+ $url = array_shift($args);
+ return vsprintf("<a$id class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
+ }
+
+ function get_small_link() {
+ /* Args are <alt text>, <format>, [<stuff> ...] */
+ $args = func_get_args();
+ array_unshift($args, null);
+ return call_user_func_array("get_small_link_with_id", $args);
+ }
+
+ function small_link() {
+ echo call_user_func_array("get_small_link", func_get_args());
+ }
+
+ function check_dates($description, $from, $to, $mandatory_from = true, $mandatory_to = true) {
+ $Description = ucfirst($description);
+ if ($from || $mandatory_from) {
+ list($y, $m, $d) = explode('-', $from);
+ if (! checkdate($m, $d, $y)) {
+ echo "<p>Invalid $description start date!</p>\n";
+ return false;