This repository was archived by the owner on May 5, 2021. It is now read-only.
forked from DaSchTour/AgeParse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgeParse.body.php
More file actions
163 lines (146 loc) · 7.13 KB
/
AgeParse.body.php
File metadata and controls
163 lines (146 loc) · 7.13 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Calculate the actual age based on information provided by the {{age}} request.
* Each attribute is already parsed so this function is only responsible for the
* actual calculation.
**/
function calculateAge($from,$to,$format,$left,$right,$errbox,$zeronegatives) {
$retval = "";
$bError = true; // error occurred in attributes; false = no error; true = error
// process the attributes
if (strlen($from) == 0) {
$retval = wfMessage('required-from')->text(); // FROM is required, even if from $sToday
} elseif (strlen($to) == 0) {
$retval = wfMessage('required-to')->text(); // TO is required, event if from $sToday
} elseif ($from == $to) {
$retval = '0 ' . wfMessage('dd-plural')->text(); // no need to calculate anything, they are the same date
} elseif ($zeronegatives && $to < $from) {
$retval = '0 ' . wfMessage('dd-plural')->text(); // TO comes before FROM, so we use '0 days'
} else {
$aDate = array();
$aDate = explode('-',$from); // separate pages in format [<era>-]<yyyy>-<mm>-<dd>
$first = array();
$first['year'] = $aDate[0] *1; // '*1' makes sure it is a number (makes some versions of PHP happy)
$first['month'] = $aDate[1] *1;
$first['day'] = $aDate[2] *1;
$aDate = explode('-',$to); // separate pages in format [<era>-]<yyyy>-<mm>-<dd>
$last = array();
$last['year'] = $aDate[0] *1; // '*1' makes sure it is a number (makes some versions of PHP happy)
$last['month'] = $aDate[1] *1;
$last['day'] = $aDate[2] *1;
// ok, ready to calculate answer
$diff = date_difference($first,$last);
if (is_array($diff)) { // got something, build answer
$bError = false; // turn off error indicator
if ($diff['years'] > 0) { // year included in any format
$retval .= $diff['years'] . ' ' . ($diff['years'] == 1 ? wfMessage('yy-single')->text() : wfMessage('yy-plural')->text());
}
if ($diff['months'] > 0 && ($format == wfMessage('ymd')->text() || $format == wfMessage('ym')->text())) {
if (strlen($retval) > 0) $retval .= wfMessage('answer-sep')->text();
$retval .= $diff['months'] . ' ' . ($diff['months'] == 1 ? wfMessage('mm-single')->text() : wfMessage('mm-plural')->text());
}
if ($diff['days'] > 0 && ($format == wfMessage('ymd')->text())) {
if (strlen($retval) > 0) $retval .= wfMessage('answer-sep')->text();
$retval .= $diff['days'] . ' ' . ($diff['days'] == 1 ? wfMessage('dd-single')->text() : wfMessage('dd-plural')->text());
}
} else {
$retval = $diff; // some error message from date_difference()
}
}
// prepare answer
if (strlen($retval) == 0)
$retval = wfMessage('catchall')->text();
if (strlen($left) > 0 || strlen($right) > 0)
$retval = $left . $retval . $right;
if ($bError && $errbox)
$retval = '<div class="'.wfMessage('errorbox')->text().'">'.$retval.'</div>'; // 'errorbox' is found in style sheet (CSS) so it is fixed (not translated)
return $retval;
}
/**
* Convenience function to create date in "YYYYMMDD" format without punctuation (-).
**/
function smoothdate ( $year, $month, $day ) {
return sprintf ('%04d', $year) . sprintf ('%02d', $month) . sprintf ('%02d', $day);
}
/**
* Date Difference performs the actual calculations. This function is based on a function
* posted at http://www.tek-tips.com/faqs.cfm?fid=3493 on 2003-04-24 as 'faq434-3493'.
*
* == INPUT ==
* $first is FROM or STARTING date
* $second is TO or ENDING date
* Both arguments must be an associative array as follows:
* array ( 'year' => year_value, 'month' => month_value, 'day' => day_value )
*
* == RULES ==
* It does not make use of 32-bit unix timestamps, so it will work for dates
* outside the range 1970-01-01 through 2038-01-19. This function works by
* taking the earlier date finding the maximum number of times it can
* increment the years, months, and days (in that order) before reaching
* the second date. The function does take yeap years into account, but does
* not take into account the 10 days removed from the calendar (specifically
* October 5 through October 14, 1582) by Pope Gregory XIII to fix calendar drift.
*
* The first input array is the earlier date, the second the later date. It
* will check to see that the two dates are well-formed, and that the first
* date is earlier than the second.
*
* == OUTPUT ==
* If successful, function returns an associative array as follows:
* array ( 'years' => years_different, 'months' => months_different, 'days' => days_different )
*
* If an error occurred, function returns a string value.
**/
function date_difference ( $first, $second ) {
$month_lengths = array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$retval = "";
if (!checkdate($first['month'], $first['day'], $first['year'])) {
$retval = wfMessage('invalid-from',$first['year'],$first['month'],$first['day'])->text();
} elseif (!checkdate($second['month'], $second['day'], $second['year'])) {
$retval = wfMessage('invalid-to',$second['year'],$second['month'],$second['day'])->text();
} elseif (mktime(0,0,0,$first['month'],$first['day'],$first['year']) > mktime(0,0,0,$second['month'],$second['day'],$second['year'])) {
$retval = wfMessage('from-first',$first['year'], $first['month'], $first['day'], $second['year'], $second['month'], $second['day'])->text();
} else {
$start = smoothdate ($first['year'], $first['month'], $first['day']);
$target = smoothdate ($second['year'], $second['month'], $second['day']);
if ($start <= $target) {
$add_year = 0;
while (smoothdate ($first['year']+ 1, $first['month'], $first['day']) <= $target) {
$add_year++;
$first['year']++;
} //while years
$add_month = 0;
while (smoothdate ($first['year'], $first['month'] + 1, $first['day']) <= $target) {
$add_month++;
$first['month']++;
if ($first['month'] > 12) {
$first['year']++;
$first['month'] = 1;
}
} //while months
$add_day = 0;
while (smoothdate ($first['year'], $first['month'], $first['day'] + 1) <= $target) {
if (($first['year'] % 100 == 0) && ($first['year'] % 400 == 0)) {
$month_lengths[1] = 29; //leap year adjustment
} else {
if ($first['year'] % 4 == 0) {
$month_lengths[1] = 29; // leap year adjustment
}
}
$add_day++;
$first['day']++;
if ($first['day'] > $month_lengths[$first['month'] - 1]) {
$first['month']++;
$first['day'] = 1;
if ($first['month'] > 12) {
$first['month'] = 1;
}
}
} // while days
$retval = array ('years' => $add_year, 'months' => $add_month, 'days' => $add_day);
} else {
$retval = wfMessage('from-first',$first['year'], $first['month'], $first['day'], $second['year'], $second['month'], $second['day'])->text();
}
}// validation ok
return $retval;
}