date_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Date Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/date_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * Get "now" time
00032  *
00033  * Returns time() or its GMT equivalent based on the config file preference
00034  *
00035  * @access      public
00036  * @return      integer
00037  */     
00038 if ( ! function_exists('now'))
00039 {
00040         function now()
00041         {
00042                 $CI =& get_instance();
00043         
00044                 if (strtolower($CI->config->item('time_reference')) == 'gmt')
00045                 {
00046                         $now = time();
00047                         $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
00048         
00049                         if (strlen($system_time) < 10)
00050                         {
00051                                 $system_time = time();
00052                                 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
00053                         }
00054         
00055                         return $system_time;
00056                 }
00057                 else
00058                 {
00059                         return time();
00060                 }
00061         }
00062 }
00063         
00064 // ------------------------------------------------------------------------
00065 
00066 /**
00067  * Convert MySQL Style Datecodes
00068  *
00069  * This function is identical to PHPs date() function,
00070  * except that it allows date codes to be formatted using
00071  * the MySQL style, where each code letter is preceded
00072  * with a percent sign:  %Y %m %d etc...
00073  *
00074  * The benefit of doing dates this way is that you don't
00075  * have to worry about escaping your text letters that
00076  * match the date codes.
00077  *
00078  * @access      public
00079  * @param       string
00080  * @param       integer
00081  * @return      integer
00082  */     
00083 if ( ! function_exists('mdate'))
00084 {
00085         function mdate($datestr = '', $time = '')
00086         {
00087                 if ($datestr == '')
00088                         return '';
00089         
00090                 if ($time == '')
00091                         $time = now();
00092                 
00093                 $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
00094                 return date($datestr, $time);
00095         }
00096 }
00097         
00098 // ------------------------------------------------------------------------
00099 
00100 /**
00101  * Standard Date
00102  *
00103  * Returns a date formatted according to the submitted standard.
00104  *
00105  * @access      public
00106  * @param       string  the chosen format
00107  * @param       integer Unix timestamp
00108  * @return      string
00109  */     
00110 if ( ! function_exists('standard_date'))
00111 {
00112         function standard_date($fmt = 'DATE_RFC822', $time = '')
00113         {
00114                 $formats = array(
00115                                                 'DATE_ATOM'             =>      '%Y-%m-%dT%H:%i:%s%Q',
00116                                                 'DATE_COOKIE'   =>      '%l, %d-%M-%y %H:%i:%s UTC',
00117                                                 'DATE_ISO8601'  =>      '%Y-%m-%dT%H:%i:%s%O',
00118                                                 'DATE_RFC822'   =>      '%D, %d %M %y %H:%i:%s %O',
00119                                                 'DATE_RFC850'   =>      '%l, %d-%M-%y %H:%m:%i UTC',
00120                                                 'DATE_RFC1036'  =>      '%D, %d %M %y %H:%i:%s %O',
00121                                                 'DATE_RFC1123'  =>      '%D, %d %M %Y %H:%i:%s %O',
00122                                                 'DATE_RSS'              =>      '%D, %d %M %Y %H:%i:%s %O',
00123                                                 'DATE_W3C'              =>      '%Y-%m-%dT%H:%i:%s%Q'
00124                                                 );
00125 
00126                 if ( ! isset($formats[$fmt]))
00127                 {
00128                         return FALSE;
00129                 }
00130         
00131                 return mdate($formats[$fmt], $time);
00132         }
00133 }
00134         
00135 // ------------------------------------------------------------------------
00136 
00137 /**
00138  * Timespan
00139  *
00140  * Returns a span of seconds in this format:
00141  *      10 days 14 hours 36 minutes 47 seconds
00142  *
00143  * @access      public
00144  * @param       integer a number of seconds
00145  * @param       integer Unix timestamp
00146  * @return      integer
00147  */     
00148 if ( ! function_exists('timespan'))
00149 {
00150         function timespan($seconds = 1, $time = '')
00151         {
00152                 $CI =& get_instance();
00153                 $CI->lang->load('date');
00154 
00155                 if ( ! is_numeric($seconds))
00156                 {
00157                         $seconds = 1;
00158                 }
00159         
00160                 if ( ! is_numeric($time))
00161                 {
00162                         $time = time();
00163                 }
00164         
00165                 if ($time <= $seconds)
00166                 {
00167                         $seconds = 1;
00168                 }
00169                 else
00170                 {
00171                         $seconds = $time - $seconds;
00172                 }
00173                 
00174                 $str = '';
00175                 $years = floor($seconds / 31536000);
00176         
00177                 if ($years > 0)
00178                 {       
00179                         $str .= $years.' '.$CI->lang->line((($years     > 1) ? 'date_years' : 'date_year')).', ';
00180                 }       
00181         
00182                 $seconds -= $years * 31536000;
00183                 $months = floor($seconds / 2628000);
00184         
00185                 if ($years > 0 OR $months > 0)
00186                 {
00187                         if ($months > 0)
00188                         {       
00189                                 $str .= $months.' '.$CI->lang->line((($months   > 1) ? 'date_months' : 'date_month')).', ';
00190                         }       
00191         
00192                         $seconds -= $months * 2628000;
00193                 }
00194 
00195                 $weeks = floor($seconds / 604800);
00196         
00197                 if ($years > 0 OR $months > 0 OR $weeks > 0)
00198                 {
00199                         if ($weeks > 0)
00200                         {       
00201                                 $str .= $weeks.' '.$CI->lang->line((($weeks     > 1) ? 'date_weeks' : 'date_week')).', ';
00202                         }
00203                 
00204                         $seconds -= $weeks * 604800;
00205                 }                       
00206 
00207                 $days = floor($seconds / 86400);
00208         
00209                 if ($months > 0 OR $weeks > 0 OR $days > 0)
00210                 {
00211                         if ($days > 0)
00212                         {       
00213                                 $str .= $days.' '.$CI->lang->line((($days       > 1) ? 'date_days' : 'date_day')).', ';
00214                         }
00215         
00216                         $seconds -= $days * 86400;
00217                 }
00218         
00219                 $hours = floor($seconds / 3600);
00220         
00221                 if ($days > 0 OR $hours > 0)
00222                 {
00223                         if ($hours > 0)
00224                         {
00225                                 $str .= $hours.' '.$CI->lang->line((($hours     > 1) ? 'date_hours' : 'date_hour')).', ';
00226                         }
00227                 
00228                         $seconds -= $hours * 3600;
00229                 }
00230         
00231                 $minutes = floor($seconds / 60);
00232         
00233                 if ($days > 0 OR $hours > 0 OR $minutes > 0)
00234                 {
00235                         if ($minutes > 0)
00236                         {       
00237                                 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
00238                         }
00239                 
00240                         $seconds -= $minutes * 60;
00241                 }
00242         
00243                 if ($str == '')
00244                 {
00245                         $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
00246                 }
00247                         
00248                 return substr(trim($str), 0, -1);
00249         }
00250 }
00251         
00252 // ------------------------------------------------------------------------
00253 
00254 /**
00255  * Number of days in a month
00256  *
00257  * Takes a month/year as input and returns the number of days
00258  * for the given month/year. Takes leap years into consideration.
00259  *
00260  * @access      public
00261  * @param       integer a numeric month
00262  * @param       integer a numeric year
00263  * @return      integer
00264  */     
00265 if ( ! function_exists('days_in_month'))
00266 {
00267         function days_in_month($month = 0, $year = '')
00268         {
00269                 if ($month < 1 OR $month > 12)
00270                 {
00271                         return 0;
00272                 }
00273         
00274                 if ( ! is_numeric($year) OR strlen($year) != 4)
00275                 {
00276                         $year = date('Y');
00277                 }
00278         
00279                 if ($month == 2)
00280                 {
00281                         if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
00282                         {
00283                                 return 29;
00284                         }
00285                 }
00286 
00287                 $days_in_month  = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
00288                 return $days_in_month[$month - 1];
00289         }
00290 }
00291         
00292 // ------------------------------------------------------------------------
00293 
00294 /**
00295  * Converts a local Unix timestamp to GMT
00296  *
00297  * @access      public
00298  * @param       integer Unix timestamp
00299  * @return      integer
00300  */     
00301 if ( ! function_exists('local_to_gmt'))
00302 {
00303         function local_to_gmt($time = '')
00304         {
00305                 if ($time == '')
00306                         $time = time();
00307         
00308                 return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
00309         }
00310 }
00311         
00312 // ------------------------------------------------------------------------
00313 
00314 /**
00315  * Converts GMT time to a localized value
00316  *
00317  * Takes a Unix timestamp (in GMT) as input, and returns
00318  * at the local value based on the timezone and DST setting
00319  * submitted
00320  *
00321  * @access      public
00322  * @param       integer Unix timestamp
00323  * @param       string  timezone
00324  * @param       bool    whether DST is active
00325  * @return      integer
00326  */     
00327 if ( ! function_exists('gmt_to_local'))
00328 {
00329         function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
00330         {                       
00331                 if ($time == '')
00332                 {
00333                         return now();
00334                 }
00335         
00336                 $time += timezones($timezone) * 3600;
00337 
00338                 if ($dst == TRUE)
00339                 {
00340                         $time += 3600;
00341                 }
00342         
00343                 return $time;
00344         }
00345 }
00346         
00347 // ------------------------------------------------------------------------
00348 
00349 /**
00350  * Converts a MySQL Timestamp to Unix
00351  *
00352  * @access      public
00353  * @param       integer Unix timestamp
00354  * @return      integer
00355  */     
00356 if ( ! function_exists('mysql_to_unix'))
00357 {
00358         function mysql_to_unix($time = '')
00359         {
00360                 // We'll remove certain characters for backward compatibility
00361                 // since the formatting changed with MySQL 4.1
00362                 // YYYY-MM-DD HH:MM:SS
00363         
00364                 $time = str_replace('-', '', $time);
00365                 $time = str_replace(':', '', $time);
00366                 $time = str_replace(' ', '', $time);
00367         
00368                 // YYYYMMDDHHMMSS
00369                 return  mktime(
00370                                                 substr($time, 8, 2),
00371                                                 substr($time, 10, 2),
00372                                                 substr($time, 12, 2),
00373                                                 substr($time, 4, 2),
00374                                                 substr($time, 6, 2),
00375                                                 substr($time, 0, 4)
00376                                                 );
00377         }
00378 }
00379         
00380 // ------------------------------------------------------------------------
00381 
00382 /**
00383  * Unix to "Human"
00384  *
00385  * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
00386  *
00387  * @access      public
00388  * @param       integer Unix timestamp
00389  * @param       bool    whether to show seconds
00390  * @param       string  format: us or euro
00391  * @return      string
00392  */     
00393 if ( ! function_exists('unix_to_human'))
00394 {
00395         function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
00396         {
00397                 $r  = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
00398                 
00399                 if ($fmt == 'us')
00400                 {
00401                         $r .= date('h', $time).':'.date('i', $time);
00402                 }
00403                 else
00404                 {
00405                         $r .= date('H', $time).':'.date('i', $time);
00406                 }
00407         
00408                 if ($seconds)
00409                 {
00410                         $r .= ':'.date('s', $time);
00411                 }
00412         
00413                 if ($fmt == 'us')
00414                 {
00415                         $r .= ' '.date('A', $time);
00416                 }
00417                 
00418                 return $r;
00419         }
00420 }
00421         
00422 // ------------------------------------------------------------------------
00423 
00424 /**
00425  * Convert "human" date to GMT
00426  *
00427  * Reverses the above process
00428  *
00429  * @access      public
00430  * @param       string  format: us or euro
00431  * @return      integer
00432  */     
00433 if ( ! function_exists('human_to_unix'))
00434 {
00435         function human_to_unix($datestr = '')
00436         {
00437                 if ($datestr == '')
00438                 {
00439                         return FALSE;
00440                 }
00441         
00442                 $datestr = trim($datestr);
00443                 $datestr = preg_replace("/\040+/", "\040", $datestr);
00444 
00445                 if ( ! ereg("^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\040[0-9]{1,2}:[0-9]{1,2}.*$", $datestr))
00446                 {
00447                         return FALSE;
00448                 }
00449 
00450                 $split = preg_split("/\040/", $datestr);
00451 
00452                 $ex = explode("-", $split['0']);
00453         
00454                 $year  = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
00455                 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1']  : $ex['1'];
00456                 $day   = (strlen($ex['2']) == 1) ? '0'.$ex['2']  : $ex['2'];
00457 
00458                 $ex = explode(":", $split['1']);
00459         
00460                 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
00461                 $min  = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
00462 
00463                 if (isset($ex['2']) AND ereg("[0-9]{1,2}", $ex['2']))
00464                 {
00465                         $sec  = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
00466                 }
00467                 else
00468                 {
00469                         // Unless specified, seconds get set to zero.
00470                         $sec = '00';
00471                 }
00472         
00473                 if (isset($split['2']))
00474                 {
00475                         $ampm = strtolower($split['2']);
00476                 
00477                         if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
00478                                 $hour = $hour + 12;
00479                         
00480                         if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
00481                                 $hour =  '00';
00482                         
00483                         if (strlen($hour) == 1)
00484                                 $hour = '0'.$hour;
00485                 }
00486                         
00487                 return mktime($hour, $min, $sec, $month, $day, $year);
00488         }
00489 }
00490         
00491 // ------------------------------------------------------------------------
00492 
00493 /**
00494  * Timezone Menu
00495  *
00496  * Generates a drop-down menu of timezones.
00497  *
00498  * @access      public
00499  * @param       string  timezone
00500  * @param       string  classname
00501  * @param       string  menu name
00502  * @return      string
00503  */     
00504 if ( ! function_exists('timezone_menu'))
00505 {
00506         function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
00507         {
00508                 $CI =& get_instance();
00509                 $CI->lang->load('date');
00510         
00511                 if ($default == 'GMT')
00512                         $default = 'UTC';
00513 
00514                 $menu = '<select name="'.$name.'"';
00515         
00516                 if ($class != '')
00517                 {
00518                         $menu .= ' class="'.$class.'"';
00519                 }
00520         
00521                 $menu .= ">\n";
00522         
00523                 foreach (timezones() as $key => $val)
00524                 {
00525                         $selected = ($default == $key) ? " selected='selected'" : '';
00526                         $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
00527                 }
00528 
00529                 $menu .= "</select>";
00530 
00531                 return $menu;
00532         }
00533 }
00534         
00535 // ------------------------------------------------------------------------
00536 
00537 /**
00538  * Timezones
00539  *
00540  * Returns an array of timezones.  This is a helper function
00541  * for various other ones in this library
00542  *
00543  * @access      public
00544  * @param       string  timezone
00545  * @return      string
00546  */     
00547 if ( ! function_exists('timezones'))
00548 {
00549         function timezones($tz = '')
00550         {
00551                 // Note: Don't change the order of these even though
00552                 // some items appear to be in the wrong order
00553                 
00554                 $zones = array(
00555                                                 'UM12' => -12,
00556                                                 'UM11' => -11,
00557                                                 'UM10' => -10,
00558                                                 'UM9'  => -9,
00559                                                 'UM8'  => -8,
00560                                                 'UM7'  => -7,
00561                                                 'UM6'  => -6,
00562                                                 'UM5'  => -5,
00563                                                 'UM4'  => -4,
00564                                                 'UM25' => -2.5,
00565                                                 'UM3'  => -3,
00566                                                 'UM2'  => -2,
00567                                                 'UM1'  => -1,
00568                                                 'UTC'  => 0,
00569                                                 'UP1'  => +1,
00570                                                 'UP2'  => +2,
00571                                                 'UP3'  => +3,
00572                                                 'UP25' => +2.5,
00573                                                 'UP4'  => +4,
00574                                                 'UP35' => +3.5,
00575                                                 'UP5'  => +5,
00576                                                 'UP45' => +4.5,
00577                                                 'UP6'  => +6,
00578                                                 'UP7'  => +7,
00579                                                 'UP8'  => +8,
00580                                                 'UP9'  => +9,
00581                                                 'UP85' => +8.5,
00582                                                 'UP10' => +10,
00583                                                 'UP11' => +11,
00584                                                 'UP12' => +12
00585                                         );
00586                                 
00587                 if ($tz == '')
00588                 {
00589                         return $zones;
00590                 }
00591         
00592                 if ($tz == 'GMT')
00593                         $tz = 'UTC';
00594         
00595                 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
00596         }
00597 }
00598 
00599 
00600 /* End of file date_helper.php */
00601 /* Location: ./system/helpers/date_helper.php */