11<?php
2- namespace minphp \Date ;
2+ namespace Minphp \Date ;
33
44/**
55 * Provides methods useful in formatting dates and date timestamps.
@@ -18,7 +18,7 @@ class Date
1818 const RFC3339 = "Y-m-d\TH:i:sP " ;
1919 const RSS = "D, d M Y H:i:s O " ;
2020 const W3C = "Y-m-d\TH:i:sP " ;
21-
21+
2222 /**
2323 * @var array Common date formats, predefined for PHP's date function, overwritable
2424 * by the constructor
@@ -30,10 +30,10 @@ class Date
3030 'year ' => "Y " ,
3131 'date_time ' => "M d y g:i:s A " ,
3232 );
33-
33+
3434 private $ timezone_from ;
3535 private $ timezone_to ;
36-
36+
3737 /**
3838 * Constructs a new Date component using the given date formats in $formats.
3939 *
@@ -50,7 +50,7 @@ public function __construct(array $formats = null, $timezone_from = null, $timez
5050 $ this ->setFormats ($ formats );
5151 $ this ->setTimezone ($ timezone_from , $ timezone_to );
5252 }
53-
53+
5454 /**
5555 * Set the current time zone to be used during date calculations
5656 *
@@ -64,7 +64,7 @@ public function setTimezone($from = null, $to = null)
6464 $ this ->timezone_to = $ to ;
6565 return $ this ;
6666 }
67-
67+
6868 /**
6969 * Sets the formats to use as the pre-defined types.
7070 *
@@ -81,7 +81,7 @@ public function setFormats(array $formats = null)
8181 $ this ->formats = array_merge ($ this ->formats , (array )$ formats );
8282 return $ this ;
8383 }
84-
84+
8585 /**
8686 * Format a date using one of the date formats provided to the constructor,
8787 * or predefined in this class.
@@ -94,7 +94,7 @@ public function cast($date, $format = "date")
9494 {
9595 return $ this ->format ((isset ($ this ->formats [$ format ]) ? $ this ->formats [$ format ] : $ format ), $ date );
9696 }
97-
97+
9898 /**
9999 * Format two dates to represent a range between them.
100100 *
@@ -121,12 +121,12 @@ public function dateRange($start, $end, $formats = null)
121121 'other ' => "F j, Y "
122122 )
123123 );
124-
124+
125125 $ formats = $ this ->mergeArrays ($ default_formats , (array )$ formats );
126-
126+
127127 $ s_date = date ("Ymd " , $ this ->toTime ($ start ));
128128 $ e_date = date ("Ymd " , $ this ->toTime ($ end ));
129-
129+
130130 if ($ s_date == $ e_date ) {
131131 // Same day
132132 return $ this ->format ($ formats ['start ' ]['same_day ' ], $ start )
@@ -144,7 +144,7 @@ public function dateRange($start, $end, $formats = null)
144144 return $ this ->format ($ formats ['start ' ]['other ' ], $ start ) . $ this ->format ($ formats ['end ' ]['other ' ], $ end );
145145 }
146146 }
147-
147+
148148 /**
149149 * Format a date using the supply date string
150150 *
@@ -162,17 +162,17 @@ public function format($format, $date = null)
162162 if ($ this ->timezone_from !== null ) {
163163 $ prev_timezone = $ this ->setDefaultTimezone ($ this ->timezone_from );
164164 }
165-
165+
166166 $ time = $ this ->toTime ($ date );
167-
167+
168168 // Set the appropriate timezone
169169 if ($ this ->timezone_to !== null ) {
170170 $ this ->setDefaultTimezone ($ this ->timezone_to );
171171 }
172-
172+
173173 // Format the date
174174 $ formatted_date = date ($ format , $ time );
175-
175+
176176 // Restore the timezone value
177177 if (isset ($ prev_timezone )) {
178178 $ this ->setDefaultTimezone ($ prev_timezone );
@@ -181,7 +181,7 @@ public function format($format, $date = null)
181181 }
182182 return null ;
183183 }
184-
184+
185185 /**
186186 * Convert a date string to Unix time
187187 *
@@ -195,7 +195,7 @@ public function toTime($date)
195195 }
196196 return $ date ;
197197 }
198-
198+
199199 /**
200200 * Returns an array of months in key/value pairs
201201 *
@@ -214,7 +214,7 @@ public function getMonths($start = 1, $end = 12, $key_format = "m", $value_forma
214214 }
215215 return $ months ;
216216 }
217-
217+
218218 /**
219219 * Returns an array of keys in key/value pairs
220220 *
@@ -233,7 +233,7 @@ public function getYears($start, $end, $key_format = "y", $value_format = "Y")
233233 }
234234 return $ years ;
235235 }
236-
236+
237237 /**
238238 * Sets the default timezone
239239 *
@@ -248,7 +248,7 @@ private function setDefaultTimezone($timezone)
248248 }
249249 return $ cur_timezone ;
250250 }
251-
251+
252252 /**
253253 * Retrieve all timezones or those for a specific country
254254 *
@@ -259,10 +259,10 @@ private function setDefaultTimezone($timezone)
259259 */
260260 public function getTimezones ($ country = null )
261261 {
262-
262+
263263 // Hold the array of timezone data
264264 $ tz_data = array ();
265-
265+
266266 $ accepted_zones = array_flip (array (
267267 "Africa " ,
268268 "America " ,
@@ -276,36 +276,36 @@ public function getTimezones($country = null)
276276 "Pacific " ,
277277 "UTC "
278278 ));
279-
279+
280280 $ listing = DateTimeZone::listIdentifiers (DateTimeZone::PER_COUNTRY , $ country );
281281 $ num_listings = count ($ listing );
282-
282+
283283 // Associate each timezone identifier with its meta data
284284 for ($ i =0 ; $ i <$ num_listings ; $ i ++) {
285285 // Convert timezone identifier to timezone array
286286 $ zone = new DateTimeZone ($ listing [$ i ]);
287287 $ zone_info = $ zone ->getTransitions (time (), time ());
288-
288+
289289 $ timezone = $ this ->timezoneFromIdentifier ($ zone_info [0 ], $ listing [$ i ]);
290290 $ primary_zone_name = isset ($ timezone ['zone ' ][0 ]) ? $ timezone ['zone ' ][0 ] : false ;
291-
291+
292292 // Only allow accepted zones into the listing
293293 if (!isset ($ accepted_zones [$ primary_zone_name ])) {
294294 continue ;
295295 }
296-
296+
297297 // Set the timezone to appear under its primary location
298298 $ tz_data [$ primary_zone_name ][] = $ timezone ;
299299 }
300-
300+
301301 // Sort each section by UTC offset
302302 foreach ($ tz_data as $ zone => $ data ) {
303303 $ this ->insertionSort ($ tz_data [$ zone ], "offset " );
304304 }
305-
305+
306306 return $ tz_data ;
307307 }
308-
308+
309309 /**
310310 * Constructs the timezone meta data using the given timezone and its identifier
311311 *
@@ -326,24 +326,24 @@ public function getTimezones($country = null)
326326 private function timezoneFromIdentifier (&$ zone_info , $ identifier )
327327 {
328328 $ zone = explode ('/ ' , $ identifier , 2 );
329-
329+
330330 $ offset = isset ($ zone_info ['offset ' ]) ? $ zone_info ['offset ' ] : 0 ; // offset
331-
331+
332332 $ offset_h = str_pad (abs ((int )($ offset /3600 )), 2 , '0 ' , STR_PAD_LEFT ); // offset in hours
333333 $ offset_h = ($ offset < 0 ? true : false ? "- " : "+ " ) . $ offset_h ;
334334 $ offset_m = str_pad (abs ((int )(($ offset /60 )%60 )), 2 , '0 ' , STR_PAD_LEFT ); // offset in mins
335-
335+
336336 $ timezone = array (
337337 'id ' => $ identifier ,
338338 'name ' => str_replace ('_ ' , ' ' , isset ($ zone [1 ]) ? $ zone [1 ] : $ zone [0 ]),
339339 'offset ' => (int )$ offset ,
340340 'utc ' => $ offset_h . ": " . $ offset_m . (isset ($ zone_info ['isdst ' ]) && $ zone_info ['isdst ' ] ? " DST " : "" ),
341341 'zone ' => $ zone
342342 );
343-
343+
344344 return $ timezone ;
345345 }
346-
346+
347347 /**
348348 * Insertion sort algorithm for numerically indexed arrays with string indexed elements.
349349 * Will sort items in $array based on values in the $key index. Sorts arrays in place.
@@ -357,7 +357,7 @@ private static function insertionSort(&$array, $key)
357357 self ::insertSortInsert ($ array , $ i , $ array [$ i ], $ key );
358358 }
359359 }
360-
360+
361361 /**
362362 * Insertion sort in inserter. Performs comparison and insertion for the given
363363 * element within the given array.
@@ -375,7 +375,7 @@ private static function insertSortInsert(&$array, $length, $element, $key)
375375 }
376376 $ array [$ i +1 ] = $ element ;
377377 }
378-
378+
379379 /**
380380 * Extends one array using another to overwrite existing values. Recursively merges
381381 * data.
@@ -386,7 +386,7 @@ private static function insertSortInsert(&$array, $length, $element, $key)
386386 */
387387 private function mergeArrays (array $ arr1 , array $ arr2 )
388388 {
389-
389+
390390 foreach ($ arr2 as $ key => $ value ) {
391391 if (array_key_exists ($ key , $ arr1 ) && is_array ($ value )) {
392392 $ arr1 [$ key ] = $ this ->mergeArrays ($ arr1 [$ key ], $ arr2 [$ key ]);
0 commit comments