commit | author | age
|
8bd4d9
|
1 |
<?php |
JK |
2 |
/** |
0868e0
|
3 |
* Klasa obsługująca polskie wyrażenia określające daty |
8bd4d9
|
4 |
* @todo Potrzebna jest funkcja, które będzie wyciągać datę z początku lub końca danego ciągu. Patrz: {@link tv::parse_date()} |
JK |
5 |
*/ |
|
6 |
class calendar { |
|
7 |
/** |
0868e0
|
8 |
* Na podstawie podanej daty zwraca uniksowy znacznik czasu |
8bd4d9
|
9 |
* @param string $date Data w formacie "naturalnym". Np. jutro, 1 stycznia, piątek |
0868e0
|
10 |
* @return int Uniksowy znacznik czasu - liczba sekund od północy 1 stycznia 1970 |
JK |
11 |
* @see http://www.php.net/date |
8bd4d9
|
12 |
*/ |
JK |
13 |
static function parse_date($date) { |
|
14 |
$date = funcs::utfToAscii($date); |
|
15 |
switch($date) { |
|
16 |
case 'jutro': |
|
17 |
return strtotime('tomorrow'); |
|
18 |
case 'za tydzien': |
|
19 |
return strtotime('+1 week'); |
|
20 |
case 'pojutrze': |
|
21 |
case 'po jutrze': |
|
22 |
return strtotime('tomorrow +1 day'); |
|
23 |
case 'dzis': |
|
24 |
return mktime(0, 0, 0); |
|
25 |
case 'teraz': |
|
26 |
case '': |
|
27 |
return time(); |
|
28 |
case 'wczoraj': |
|
29 |
return strtotime('yesterday'); |
|
30 |
case 'przedwczoraj': |
|
31 |
case 'przed wczoraj': |
|
32 |
return strtotime('yesterday -1 days'); |
|
33 |
case 'poniedzialek': |
|
34 |
return strtotime('monday'); |
|
35 |
case 'wtorek': |
|
36 |
return strtotime('tuesday'); |
|
37 |
case 'sroda': |
|
38 |
return strtotime('wednesday'); |
|
39 |
case 'czwartek': |
|
40 |
return strtotime('thursday'); |
|
41 |
case 'piatek': |
|
42 |
return strtotime('friday'); |
|
43 |
case 'sobota': |
|
44 |
return strtotime('saturday'); |
|
45 |
case 'niedziela': |
|
46 |
return strtotime('sunday'); |
|
47 |
} |
|
48 |
|
|
49 |
if(substr($date, 0, 2)=='za') { |
|
50 |
$czego = array( |
|
51 |
'sekunde' => 'second', |
|
52 |
'sekund' => 'seconds', |
|
53 |
'sekundy' => 'seconds', |
|
54 |
'minuta' => 'minute', |
|
55 |
'minut' => 'minutes', |
|
56 |
'minuty' => 'minutes', |
|
57 |
'godzine' => 'hour', |
|
58 |
'godzin' => 'hours', |
|
59 |
'godziny' => 'hours', |
|
60 |
'dzien' => 'day', |
|
61 |
'dni' => 'days', |
|
62 |
'miesiac' => 'month', |
|
63 |
'miesiecy' => 'months', |
|
64 |
'miesiace' => 'months', |
|
65 |
'rok' => 'year', |
|
66 |
'lat' => 'years', |
|
67 |
'lata' => 'years', |
|
68 |
); |
|
69 |
$date = trim(substr($date, 2)); |
|
70 |
|
|
71 |
$ile = array( |
|
72 |
'zero' => 0, |
|
73 |
'jeden' => 1, |
|
74 |
'dwa' => 2, |
|
75 |
'trzy' => 3, |
|
76 |
'cztery' => 4, |
|
77 |
'piec' => 5, |
|
78 |
'szesc' => 6, |
|
79 |
'siedem' => 7, |
|
80 |
'osiem' => 8, |
|
81 |
'dziewiec' => 9, |
|
82 |
'dziesiec' => 10, |
|
83 |
); |
|
84 |
foreach($ile as $key => $num) { |
|
85 |
if(substr($date, 0, strlen($key)) == $key) { |
|
86 |
$ile = $num; |
|
87 |
$done = TRUE; |
|
88 |
$date = trim(substr($date, strlen($key))); |
|
89 |
break; |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
if($done) { |
|
94 |
} |
|
95 |
elseif(!is_numeric(substr($date, 0, 1))) { |
|
96 |
$ile = 1; |
|
97 |
} |
|
98 |
else |
|
99 |
{ |
|
100 |
$ile = (int)$date; |
|
101 |
$date = trim(substr($date, strlen($ile))); |
|
102 |
} |
|
103 |
|
|
104 |
$czego = $czego[$date]; |
|
105 |
if(!$czego) return FALSE; |
|
106 |
|
|
107 |
return strtotime('+'.$ile.' '.$czego); |
|
108 |
} |
|
109 |
|
|
110 |
if(preg_match('/([0-9]{1,2})\.([0-9]{1,2}).([0-9]{2,4})/', $date, $test)) { |
|
111 |
return mktime(0, 0, 0, $test[2], $test[1], $test[3]); |
|
112 |
} |
|
113 |
elseif(preg_match('/([0-9]{1,2})\.([0-9]{1,2})/', $date, $test)) { |
|
114 |
return mktime(0, 0, 0, $test[2], $test[1]); |
|
115 |
} |
|
116 |
elseif(preg_match('/([0-9]{1,2})\-([0-9]{1,2})\-([0-9]{2,4})/', $date, $test)) { |
|
117 |
return mktime(0, 0, 0, $test[2], $test[1], $test[3]); |
|
118 |
} |
|
119 |
elseif(preg_match('/([0-9]{1,2})\s+([a-z]{3,13})\s+([0-9]{2,4})/', $date, $test) || preg_match('/([0-9]{1,2})\s+([a-z]{3,13})/', $date, $test)) { |
|
120 |
$array = array( |
|
121 |
'styczen' => 1, |
|
122 |
'stycznia' => 1, |
|
123 |
'luty' => 2, |
|
124 |
'lutego' => 2, |
|
125 |
'marzec' => 3, |
|
126 |
'marca' => 3, |
|
127 |
'kwiecien' => 4, |
|
128 |
'kwietnia' => 4, |
|
129 |
'maj' => 5, |
|
130 |
'maja' => 5, |
|
131 |
'czerwiec' => 6, |
|
132 |
'czerwca' => 6, |
|
133 |
'lipiec' => 7, |
|
134 |
'lipca' => 7, |
|
135 |
'sierpien' => 8, |
|
136 |
'sierpnia' => 8, |
|
137 |
'wrzesien' => 9, |
|
138 |
'wrzesnia' => 9, |
|
139 |
'pazdziernik' => 10, |
|
140 |
'pazdziernika' => 10, |
|
141 |
'listopad' => 11, |
|
142 |
'listopada' => 11, |
|
143 |
'grudzien' => 12, |
|
144 |
'grudnia' => 12, |
|
145 |
); |
|
146 |
if(!isset($array[$test[2]])) return FALSE; |
|
147 |
if(!$test[3]) $test[3] = date('Y'); |
|
148 |
return mktime(0, 0, 0, $array[$test[2]], $test[1], $test[3]); |
|
149 |
} |
|
150 |
else |
|
151 |
{ |
|
152 |
return strtotime($date); |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
?> |