/
DateTimeInput.class.php
124 lines (102 loc) · 2.9 KB
/
DateTimeInput.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
Library::import('recess.framework.forms.FormInput');
class DateTimeInput extends FormInput {
public $showDate = true;
public $showTime = true;
protected static $months = array('Jan',
'Feb',
'Mar',
'Apr',
'May',
'June',
'July',
'Aug',
'Sept',
'Oct',
'Nov',
'Dec');
protected static $meridiems = array(
self::AM,
self::PM
);
const MONTH = 'month';
const DAY = 'day';
const YEAR = 'year';
const HOUR = 'hour';
const MINUTE = 'minute';
const MERIDIEM = 'meridiem';
const AM = 'am';
const PM = 'pm';
const PM_HOURS = 12;
function getValue() {
return $this->value;
}
function getValueOrZero($array, $key) {
if(isset($array[$key]) && $array[$key] != '')
return $array[$key];
else
return 0;
}
function setValue($value) {
if(is_array($value)) {
$month = $this->getValueOrZero($value, self::MONTH);
$day = $this->getValueOrZero($value, self::DAY);
$year = $this->getValueOrZero($value, self::YEAR);
$hour = $this->getValueOrZero($value, self::HOUR);
$minute = $this->getValueOrZero($value, self::MINUTE);
$meridiem = $this->getValueOrZero($value, self::MERIDIEM);
if($meridiem == self::PM) {
$hour += self::PM_HOURS;
}
$this->value = mktime($hour,$minute,1,$month,$day,$year);
} else {
$this->value = $value;
}
}
function render() {
if($this->showDate) {
$this->printMonthInput();
$this->printDayInput();
$this->printYearInput();
}
if($this->showTime) {
$this->printHourInput();
$this->printMinuteInput();
$this->printmeridiemInput();
}
}
function printMonthInput() {
$this->printSelect($this->name . '[' . self::MONTH . ']', self::$months, date('n', $this->value));
}
function printDayInput() {
$this->printSelect($this->name . '[' . self::DAY . ']', range(1,31), date('j', $this->value));
}
function printYearInput() {
$this->printText($this->name . '[' . self::YEAR . ']', date('Y', $this->value));
}
function printHourInput() {
$this->printSelect($this->name . '[' . self::HOUR . ']', range(1,12), date('g', $this->value));
}
function printMinuteInput() {
$this->printSelect($this->name . '[' . self::MINUTE . ']', range(0,60,15), (int)date('i', $this->value));
}
function printMeridiemInput() {
$this->printSelect($this->name . '[' . self::MERIDIEM . ']', self::$meridiems, date('a', $this->value));
}
function printSelect($name, $values, $selected) {
echo '<select name="', $name, '">';
foreach($values as $key => $value) {
$key++;
echo '<option value="', $key, '"';
if($key == $selected) {
echo ' selected="selected"';
}
echo '>', $value, '</option>', "\n";
}
echo '</select>';
}
function printText($name, $value = '') {
echo '<input class="text short" name="' . $name . '" value="' . $value . '" />';
}
}
?>