Take two:
[www-register-wizard.git] / helpers / date_helper.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * CodeIgniter Date Helpers\r
20  *\r
21  * @package             CodeIgniter\r
22  * @subpackage  Helpers\r
23  * @category    Helpers\r
24  * @author              ExpressionEngine Dev Team\r
25  * @link                http://codeigniter.com/user_guide/helpers/date_helper.html\r
26  */\r
27 \r
28 // ------------------------------------------------------------------------\r
29 \r
30 /**\r
31  * Get "now" time\r
32  *\r
33  * Returns time() or its GMT equivalent based on the config file preference\r
34  *\r
35  * @access      public\r
36  * @return      integer\r
37  */     \r
38 if ( ! function_exists('now'))\r
39 {\r
40         function now()\r
41         {\r
42                 $CI =& get_instance();\r
43         \r
44                 if (strtolower($CI->config->item('time_reference')) == 'gmt')\r
45                 {\r
46                         $now = time();\r
47                         $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));\r
48         \r
49                         if (strlen($system_time) < 10)\r
50                         {\r
51                                 $system_time = time();\r
52                                 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');\r
53                         }\r
54         \r
55                         return $system_time;\r
56                 }\r
57                 else\r
58                 {\r
59                         return time();\r
60                 }\r
61         }\r
62 }\r
63         \r
64 // ------------------------------------------------------------------------\r
65 \r
66 /**\r
67  * Convert MySQL Style Datecodes\r
68  *\r
69  * This function is identical to PHPs date() function,\r
70  * except that it allows date codes to be formatted using\r
71  * the MySQL style, where each code letter is preceded\r
72  * with a percent sign:  %Y %m %d etc...\r
73  *\r
74  * The benefit of doing dates this way is that you don't\r
75  * have to worry about escaping your text letters that\r
76  * match the date codes.\r
77  *\r
78  * @access      public\r
79  * @param       string\r
80  * @param       integer\r
81  * @return      integer\r
82  */     \r
83 if ( ! function_exists('mdate'))\r
84 {\r
85         function mdate($datestr = '', $time = '')\r
86         {\r
87                 if ($datestr == '')\r
88                         return '';\r
89         \r
90                 if ($time == '')\r
91                         $time = now();\r
92                 \r
93                 $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));\r
94                 return date($datestr, $time);\r
95         }\r
96 }\r
97         \r
98 // ------------------------------------------------------------------------\r
99 \r
100 /**\r
101  * Standard Date\r
102  *\r
103  * Returns a date formatted according to the submitted standard.\r
104  *\r
105  * @access      public\r
106  * @param       string  the chosen format\r
107  * @param       integer Unix timestamp\r
108  * @return      string\r
109  */     \r
110 if ( ! function_exists('standard_date'))\r
111 {\r
112         function standard_date($fmt = 'DATE_RFC822', $time = '')\r
113         {\r
114                 $formats = array(\r
115                                                 'DATE_ATOM'             =>      '%Y-%m-%dT%H:%i:%s%Q',\r
116                                                 'DATE_COOKIE'   =>      '%l, %d-%M-%y %H:%i:%s UTC',\r
117                                                 'DATE_ISO8601'  =>      '%Y-%m-%dT%H:%i:%s%O',\r
118                                                 'DATE_RFC822'   =>      '%D, %d %M %y %H:%i:%s %O',\r
119                                                 'DATE_RFC850'   =>      '%l, %d-%M-%y %H:%m:%i UTC',\r
120                                                 'DATE_RFC1036'  =>      '%D, %d %M %y %H:%i:%s %O',\r
121                                                 'DATE_RFC1123'  =>      '%D, %d %M %Y %H:%i:%s %O',\r
122                                                 'DATE_RSS'              =>      '%D, %d %M %Y %H:%i:%s %O',\r
123                                                 'DATE_W3C'              =>      '%Y-%m-%dT%H:%i:%s%Q'\r
124                                                 );\r
125 \r
126                 if ( ! isset($formats[$fmt]))\r
127                 {\r
128                         return FALSE;\r
129                 }\r
130         \r
131                 return mdate($formats[$fmt], $time);\r
132         }\r
133 }\r
134         \r
135 // ------------------------------------------------------------------------\r
136 \r
137 /**\r
138  * Timespan\r
139  *\r
140  * Returns a span of seconds in this format:\r
141  *      10 days 14 hours 36 minutes 47 seconds\r
142  *\r
143  * @access      public\r
144  * @param       integer a number of seconds\r
145  * @param       integer Unix timestamp\r
146  * @return      integer\r
147  */     \r
148 if ( ! function_exists('timespan'))\r
149 {\r
150         function timespan($seconds = 1, $time = '')\r
151         {\r
152                 $CI =& get_instance();\r
153                 $CI->lang->load('date');\r
154 \r
155                 if ( ! is_numeric($seconds))\r
156                 {\r
157                         $seconds = 1;\r
158                 }\r
159         \r
160                 if ( ! is_numeric($time))\r
161                 {\r
162                         $time = time();\r
163                 }\r
164         \r
165                 if ($time <= $seconds)\r
166                 {\r
167                         $seconds = 1;\r
168                 }\r
169                 else\r
170                 {\r
171                         $seconds = $time - $seconds;\r
172                 }\r
173                 \r
174                 $str = '';\r
175                 $years = floor($seconds / 31536000);\r
176         \r
177                 if ($years > 0)\r
178                 {       \r
179                         $str .= $years.' '.$CI->lang->line((($years     > 1) ? 'date_years' : 'date_year')).', ';\r
180                 }       \r
181         \r
182                 $seconds -= $years * 31536000;\r
183                 $months = floor($seconds / 2628000);\r
184         \r
185                 if ($years > 0 OR $months > 0)\r
186                 {\r
187                         if ($months > 0)\r
188                         {       \r
189                                 $str .= $months.' '.$CI->lang->line((($months   > 1) ? 'date_months' : 'date_month')).', ';\r
190                         }       \r
191         \r
192                         $seconds -= $months * 2628000;\r
193                 }\r
194 \r
195                 $weeks = floor($seconds / 604800);\r
196         \r
197                 if ($years > 0 OR $months > 0 OR $weeks > 0)\r
198                 {\r
199                         if ($weeks > 0)\r
200                         {       \r
201                                 $str .= $weeks.' '.$CI->lang->line((($weeks     > 1) ? 'date_weeks' : 'date_week')).', ';\r
202                         }\r
203                 \r
204                         $seconds -= $weeks * 604800;\r
205                 }                       \r
206 \r
207                 $days = floor($seconds / 86400);\r
208         \r
209                 if ($months > 0 OR $weeks > 0 OR $days > 0)\r
210                 {\r
211                         if ($days > 0)\r
212                         {       \r
213                                 $str .= $days.' '.$CI->lang->line((($days       > 1) ? 'date_days' : 'date_day')).', ';\r
214                         }\r
215         \r
216                         $seconds -= $days * 86400;\r
217                 }\r
218         \r
219                 $hours = floor($seconds / 3600);\r
220         \r
221                 if ($days > 0 OR $hours > 0)\r
222                 {\r
223                         if ($hours > 0)\r
224                         {\r
225                                 $str .= $hours.' '.$CI->lang->line((($hours     > 1) ? 'date_hours' : 'date_hour')).', ';\r
226                         }\r
227                 \r
228                         $seconds -= $hours * 3600;\r
229                 }\r
230         \r
231                 $minutes = floor($seconds / 60);\r
232         \r
233                 if ($days > 0 OR $hours > 0 OR $minutes > 0)\r
234                 {\r
235                         if ($minutes > 0)\r
236                         {       \r
237                                 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';\r
238                         }\r
239                 \r
240                         $seconds -= $minutes * 60;\r
241                 }\r
242         \r
243                 if ($str == '')\r
244                 {\r
245                         $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';\r
246                 }\r
247                         \r
248                 return substr(trim($str), 0, -1);\r
249         }\r
250 }\r
251         \r
252 // ------------------------------------------------------------------------\r
253 \r
254 /**\r
255  * Number of days in a month\r
256  *\r
257  * Takes a month/year as input and returns the number of days\r
258  * for the given month/year. Takes leap years into consideration.\r
259  *\r
260  * @access      public\r
261  * @param       integer a numeric month\r
262  * @param       integer a numeric year\r
263  * @return      integer\r
264  */     \r
265 if ( ! function_exists('days_in_month'))\r
266 {\r
267         function days_in_month($month = 0, $year = '')\r
268         {\r
269                 if ($month < 1 OR $month > 12)\r
270                 {\r
271                         return 0;\r
272                 }\r
273         \r
274                 if ( ! is_numeric($year) OR strlen($year) != 4)\r
275                 {\r
276                         $year = date('Y');\r
277                 }\r
278         \r
279                 if ($month == 2)\r
280                 {\r
281                         if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))\r
282                         {\r
283                                 return 29;\r
284                         }\r
285                 }\r
286 \r
287                 $days_in_month  = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);\r
288                 return $days_in_month[$month - 1];\r
289         }\r
290 }\r
291         \r
292 // ------------------------------------------------------------------------\r
293 \r
294 /**\r
295  * Converts a local Unix timestamp to GMT\r
296  *\r
297  * @access      public\r
298  * @param       integer Unix timestamp\r
299  * @return      integer\r
300  */     \r
301 if ( ! function_exists('local_to_gmt'))\r
302 {\r
303         function local_to_gmt($time = '')\r
304         {\r
305                 if ($time == '')\r
306                         $time = time();\r
307         \r
308                 return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));\r
309         }\r
310 }\r
311         \r
312 // ------------------------------------------------------------------------\r
313 \r
314 /**\r
315  * Converts GMT time to a localized value\r
316  *\r
317  * Takes a Unix timestamp (in GMT) as input, and returns\r
318  * at the local value based on the timezone and DST setting\r
319  * submitted\r
320  *\r
321  * @access      public\r
322  * @param       integer Unix timestamp\r
323  * @param       string  timezone\r
324  * @param       bool    whether DST is active\r
325  * @return      integer\r
326  */     \r
327 if ( ! function_exists('gmt_to_local'))\r
328 {\r
329         function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)\r
330         {                       \r
331                 if ($time == '')\r
332                 {\r
333                         return now();\r
334                 }\r
335         \r
336                 $time += timezones($timezone) * 3600;\r
337 \r
338                 if ($dst == TRUE)\r
339                 {\r
340                         $time += 3600;\r
341                 }\r
342         \r
343                 return $time;\r
344         }\r
345 }\r
346         \r
347 // ------------------------------------------------------------------------\r
348 \r
349 /**\r
350  * Converts a MySQL Timestamp to Unix\r
351  *\r
352  * @access      public\r
353  * @param       integer Unix timestamp\r
354  * @return      integer\r
355  */     \r
356 if ( ! function_exists('mysql_to_unix'))\r
357 {\r
358         function mysql_to_unix($time = '')\r
359         {\r
360                 // We'll remove certain characters for backward compatibility\r
361                 // since the formatting changed with MySQL 4.1\r
362                 // YYYY-MM-DD HH:MM:SS\r
363         \r
364                 $time = str_replace('-', '', $time);\r
365                 $time = str_replace(':', '', $time);\r
366                 $time = str_replace(' ', '', $time);\r
367         \r
368                 // YYYYMMDDHHMMSS\r
369                 return  mktime(\r
370                                                 substr($time, 8, 2),\r
371                                                 substr($time, 10, 2),\r
372                                                 substr($time, 12, 2),\r
373                                                 substr($time, 4, 2),\r
374                                                 substr($time, 6, 2),\r
375                                                 substr($time, 0, 4)\r
376                                                 );\r
377         }\r
378 }\r
379         \r
380 // ------------------------------------------------------------------------\r
381 \r
382 /**\r
383  * Unix to "Human"\r
384  *\r
385  * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM\r
386  *\r
387  * @access      public\r
388  * @param       integer Unix timestamp\r
389  * @param       bool    whether to show seconds\r
390  * @param       string  format: us or euro\r
391  * @return      string\r
392  */     \r
393 if ( ! function_exists('unix_to_human'))\r
394 {\r
395         function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')\r
396         {\r
397                 $r  = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';\r
398                 \r
399                 if ($fmt == 'us')\r
400                 {\r
401                         $r .= date('h', $time).':'.date('i', $time);\r
402                 }\r
403                 else\r
404                 {\r
405                         $r .= date('H', $time).':'.date('i', $time);\r
406                 }\r
407         \r
408                 if ($seconds)\r
409                 {\r
410                         $r .= ':'.date('s', $time);\r
411                 }\r
412         \r
413                 if ($fmt == 'us')\r
414                 {\r
415                         $r .= ' '.date('A', $time);\r
416                 }\r
417                 \r
418                 return $r;\r
419         }\r
420 }\r
421         \r
422 // ------------------------------------------------------------------------\r
423 \r
424 /**\r
425  * Convert "human" date to GMT\r
426  *\r
427  * Reverses the above process\r
428  *\r
429  * @access      public\r
430  * @param       string  format: us or euro\r
431  * @return      integer\r
432  */     \r
433 if ( ! function_exists('human_to_unix'))\r
434 {\r
435         function human_to_unix($datestr = '')\r
436         {\r
437                 if ($datestr == '')\r
438                 {\r
439                         return FALSE;\r
440                 }\r
441         \r
442                 $datestr = trim($datestr);\r
443                 $datestr = preg_replace("/\040+/", "\040", $datestr);\r
444 \r
445                 if ( ! preg_match('/^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))\r
446                 {\r
447                         return FALSE;\r
448                 }\r
449 \r
450                 $split = preg_split("/\040/", $datestr);\r
451 \r
452                 $ex = explode("-", $split['0']);\r
453         \r
454                 $year  = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];\r
455                 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1']  : $ex['1'];\r
456                 $day   = (strlen($ex['2']) == 1) ? '0'.$ex['2']  : $ex['2'];\r
457 \r
458                 $ex = explode(":", $split['1']);\r
459         \r
460                 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];\r
461                 $min  = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];\r
462 \r
463                 if (isset($ex['2']) AND ereg("[0-9]{1,2}", $ex['2']))\r
464                 {\r
465                         $sec  = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];\r
466                 }\r
467                 else\r
468                 {\r
469                         // Unless specified, seconds get set to zero.\r
470                         $sec = '00';\r
471                 }\r
472         \r
473                 if (isset($split['2']))\r
474                 {\r
475                         $ampm = strtolower($split['2']);\r
476                 \r
477                         if (substr($ampm, 0, 1) == 'p' AND $hour < 12)\r
478                                 $hour = $hour + 12;\r
479                         \r
480                         if (substr($ampm, 0, 1) == 'a' AND $hour == 12)\r
481                                 $hour =  '00';\r
482                         \r
483                         if (strlen($hour) == 1)\r
484                                 $hour = '0'.$hour;\r
485                 }\r
486                         \r
487                 return mktime($hour, $min, $sec, $month, $day, $year);\r
488         }\r
489 }\r
490         \r
491 // ------------------------------------------------------------------------\r
492 \r
493 /**\r
494  * Timezone Menu\r
495  *\r
496  * Generates a drop-down menu of timezones.\r
497  *\r
498  * @access      public\r
499  * @param       string  timezone\r
500  * @param       string  classname\r
501  * @param       string  menu name\r
502  * @return      string\r
503  */     \r
504 if ( ! function_exists('timezone_menu'))\r
505 {\r
506         function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')\r
507         {\r
508                 $CI =& get_instance();\r
509                 $CI->lang->load('date');\r
510         \r
511                 if ($default == 'GMT')\r
512                         $default = 'UTC';\r
513 \r
514                 $menu = '<select name="'.$name.'"';\r
515         \r
516                 if ($class != '')\r
517                 {\r
518                         $menu .= ' class="'.$class.'"';\r
519                 }\r
520         \r
521                 $menu .= ">\n";\r
522         \r
523                 foreach (timezones() as $key => $val)\r
524                 {\r
525                         $selected = ($default == $key) ? " selected='selected'" : '';\r
526                         $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";\r
527                 }\r
528 \r
529                 $menu .= "</select>";\r
530 \r
531                 return $menu;\r
532         }\r
533 }\r
534         \r
535 // ------------------------------------------------------------------------\r
536 \r
537 /**\r
538  * Timezones\r
539  *\r
540  * Returns an array of timezones.  This is a helper function\r
541  * for various other ones in this library\r
542  *\r
543  * @access      public\r
544  * @param       string  timezone\r
545  * @return      string\r
546  */     \r
547 if ( ! function_exists('timezones'))\r
548 {\r
549         function timezones($tz = '')\r
550         {\r
551                 // Note: Don't change the order of these even though\r
552                 // some items appear to be in the wrong order\r
553                 \r
554                 $zones = array( \r
555                                                 'UM12'          => -12,\r
556                                                 'UM11'          => -11,\r
557                                                 'UM10'          => -10,\r
558                                                 'UM95'          => -9.5,\r
559                                                 'UM9'           => -9,\r
560                                                 'UM8'           => -8,\r
561                                                 'UM7'           => -7,\r
562                                                 'UM6'           => -6,\r
563                                                 'UM5'           => -5,\r
564                                                 'UM45'          => -4.5,\r
565                                                 'UM4'           => -4,\r
566                                                 'UM35'          => -3.5,\r
567                                                 'UM3'           => -3,\r
568                                                 'UM2'           => -2,\r
569                                                 'UM1'           => -1,\r
570                                                 'UTC'           => 0,\r
571                                                 'UP1'           => +1,\r
572                                                 'UP2'           => +2,\r
573                                                 'UP3'           => +3,\r
574                                                 'UP35'          => +3.5,\r
575                                                 'UP4'           => +4,\r
576                                                 'UP45'          => +4.5,\r
577                                                 'UP5'           => +5,\r
578                                                 'UP55'          => +5.5,\r
579                                                 'UP575'         => +5.75,\r
580                                                 'UP6'           => +6,\r
581                                                 'UP65'          => +6.5,\r
582                                                 'UP7'           => +7,\r
583                                                 'UP8'           => +8,\r
584                                                 'UP875'         => +8.75,\r
585                                                 'UP9'           => +9,\r
586                                                 'UP95'          => +9.5,\r
587                                                 'UP10'          => +10,\r
588                                                 'UP105'         => +10.5,\r
589                                                 'UP11'          => +11,\r
590                                                 'UP115'         => +11.5,\r
591                                                 'UP12'          => +12,\r
592                                                 'UP1275'        => +12.75,\r
593                                                 'UP13'          => +13,\r
594                                                 'UP14'          => +14\r
595                                         );\r
596                                 \r
597                 if ($tz == '')\r
598                 {\r
599                         return $zones;\r
600                 }\r
601         \r
602                 if ($tz == 'GMT')\r
603                         $tz = 'UTC';\r
604         \r
605                 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];\r
606         }\r
607 }\r
608 \r
609 \r
610 /* End of file date_helper.php */\r
611 /* Location: ./system/helpers/date_helper.php */