-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFormat.php
279 lines (245 loc) · 7.14 KB
/
Format.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
@file
@brief Radix Output Formatting Routines
@package radix
@see http://en.wikipedia.org/wiki/Local_conventions_for_writing_telephone_numbers
*/
/**
A collection of Static Methods
*/
namespace Edoceo\Radix;
class Format
{
/**
niceDate
@param $date - date to get formatted "nicely"
@return some HTML to display.
*/
static function niceDate($date)
{
// Determines how long ago the date was/is and then how to display it nicely
// Not smart for determining time factors like time-zone, daylight-standard, leap-year, etc
if (empty($date)) {
return 'Never';
}
$ts_cmp = strtotime($date);
if (($ts_cmp <= 0) && ($date > 0) ) {
$ts_cmp = $date;
}
$ts_now = time();
// past or future doesn't matter, just the difference
$span = abs($ts_now - $ts_cmp);
$nice = null;
$full = strftime('%a %b(%m) %d, %Y',$ts_cmp);
if ($span <= 86400) { // Day
$nice = 'Today';
// return strftime('%H:%M',$ts_cmp);
} elseif ($span <= 172800) { // 2 Days
$nice = 'Yesterday';
} elseif ($span <= 604800) { // 7 Days
$nice = strftime('Last %a',$ts_cmp); // Day ##
} elseif ($span <= 2592000) { // 30 Days
$nice = strftime('%b %d',$ts_cmp); // Mon ##
} elseif ($span <= 31536000) { // 365 Days
$nice = strftime('%m/%d',$ts_cmp);
} else {
$nice = strftime('%m/%d/%y',$ts_cmp);
}
return '<span title="' . $full . '">' . $nice . '</span>';
}
/**
Returns a nicely formatted time, like what Google Mail (and many others) do
@return nicely formatted string
*/
static function niceTime($time)
{
if (empty($time)) {
return 'Never';
}
$ts_cmp = strtotime($time);
if (($ts_cmp <= 0) && ($time > 0) ) {
$ts_cmp = $time;
}
$ts_now = time();
// past or future doesn't matter, just the difference
$span = $ts_now - $ts_cmp;
$nice = null;
$full = strftime('%a %b(%m) %d, %Y',$ts_cmp);
if ($span <= 30) { // 30 Seconds
return 'a few seconds ago';
}
if ($span <= 300) { // Five Minutes
return 'a few minutes ago';
}
if ($span <= 3600) {// One Hour
return 'about ' . floor($span / 300 * 5) . ' minutes ago';
}
return self::niceDate($time);
}
/**
Formats a decimal number into 1024 base sizes to "binary prefix
@see http://en.wikipedia.org/wiki/Binary_prefix
@param $size large number
@param $fmt sprintf value of %d (size) and %s (measurement)
@return formatted string like, 21MiB
*/
static function niceSize($size,$fmt='%d %s')
{
$sizes = array('YiB', 'ZiB', 'EiB', 'PiB', 'TiB', 'GiB', 'MiB', 'KiB', 'B');
$total = count($sizes);
while($total-- && $size > 1024) $size /= 1024;
return sprintf($fmt, $size,$sizes[$total]);
}
/**
Time Span
*/
static function spanAsMMSS($s)
{
$m = floor($s/60);
$s = $s - ($m * 60);
return sprintf('%d:%02d', $m, $s);
}
/**
Time Span
*/
static function spanAsHHMMSS($s)
{
$h = floor($s / 3600);
$m = floor(($s - ($h * 3600)) / 60);
$s = $s - ($h * 3600) - ($m * 60);
return sprintf('%d:%02d:%02d', $h, $m, $s);
}
/**
Formats a Telephone Number
@see http://countrycode.org/
@param $p phone number
@param $iso2 country ISO code for formatting
*/
static function phone($p,$iso2=null)
{
$ext = null;
$num = preg_replace('/[^x,\d]+/',null,$p);
if (preg_match('/^(\d+)x(\d+)$/',$num,$m)) {
$num = $m[1];
$ext = $m[2];
}
switch (strlen($num)) {
case 6:
$ret = sprintf('%d-%d',substr($num,0,3),substr($num,3,3));
break;
case 7:
$ret = sprintf('%d-%04d',substr($num,0,3),substr($num,3,4));
break;
case 8:
$ret = sprintf('%04d-%04d',substr($num,0,4),substr($num,4,4));
break;
case 10:
switch (strtolower($iso2)) {
case 'us':
default:
// $ret = '(' . substr($num, 0, 3) . ') ' . substr($num, 3, 3) . "-".substr($num, 6, 4);
$ret = sprintf('%d-%d-%04d',
substr($num,0,3),
substr($num,3,3),
substr($num,6,4));
break;
}
default:
$ret = $num;
}
// China
if (preg_match('/^(86\d{7,11})$/',$p,$m)) {
return "+{$m[1]}";
}
// India
if (preg_match('/^(91[1-8]\d{7,11})$/',$p,$m)) {
return "+{$m[1]}";
}
// Last on the List
// Saint Martin
if (preg_match('/^(590)(590)(\d{2})(\d{2})(\d{2})/',$p,$m)) {
return "+{$m[1]} {$m[2]} {$m[3]} {$m[4]} {$m[5]}";
}
// Prepare Return Value
// $ret = $num;
// Add Extension
$ret.= (!empty($ext) ? "x$ext" : null);
return $ret;
/*
// Phones with no extension
switch(strlen($phone))
{
case 7:
$ret = substr($phone, 0, 3)."-".substr($phone, 0, -3);
break;
case 8:
$ret = substr($phone, 0, 4)."-".substr($phone, 0, -4);
break;
case 10:
$ret = "(".substr($phone, 0, 3).") ".substr($phone, 3, 3)."-".substr($phone, 6, 4);
break;
default:
$ret = $phone;
}
return $ret.$ext;
*/
// function e164_phone($x)
// {
// // Assume it's OK
// if (substr($x,0,1)=='+') {
// return $x;
// }
//
// $x = preg_replace('/[^\d]+/',null,$x);
//
// // US 7 Digit Needs Default Area Code
// if (strlen($x)==7) {
// $x = $_ENV['locale']['prefix_area'] . $x;
// }
// // US 10 Digit Needs Default Country Code
// if (strlen($x)==10) {
// $x = '+' . $_ENV['locale']['prefix_intl'] . $x;
// }
// return $x;
// }
return $num;
}
/**
Phone e164 Format
@see http://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes
@param $x Phone Number
@return e164 formatted phone number
*/
public static function phone_e164($x)
{
$x = preg_replace('/[^\d]+/',null,$x);
// US 10 Digit
if(preg_match('/^1?([2-9][0-8]\d{8})$/',$x,$m)){
return "+1{$m[1]}";
}
// Have Attempted to Sort These By Country
// China
if (preg_match('/^(86\d{7,11})$/',$x,$m)) {
return "+{$m[1]}";
}
// intl 10
if(preg_match('/^([2-9][0-9]{9})$/',$x,$m)){
return "+{$m[1]}";
}
// intl longer
if(preg_match('/^([2-9][0-9]{8,14})$/',$x,$m)){
return "+{$m[1]}";
}
// Specialty US? \d11
if(preg_match('/^([2-9]11)$/',$x,$m)){
return "+1{$m[1]}";
}
if (preg_match('/^260[1-2]\d{6}/',$x,$m)) { // Zambia
//
}
if (preg_match('/^(263{8})/',$x,$m)) { // Zimbabwe
}
return $x;
}
}