Advertisement
bkader

CodeIgniter Time Helper

Jan 1st, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. /**
  5.  * Time Helper
  6.  *
  7.  * @package     CodeIgniter
  8.  * @subpackage  Helpers
  9.  * @category    Time Helper
  10.  * @author      Kader Bouyakoub <bkader@mail.com>
  11.  * @link        @KaderBouyakoub
  12.  */
  13.  
  14. if ( ! function_exists('list_minutes'))
  15. {
  16.     /**
  17.      * List 60 minutes with optional limit
  18.      *
  19.      * @access  public
  20.      * @param   integer
  21.      * @return  array
  22.      */
  23.     function list_minutes($limit = 60)
  24.     {
  25.         $minutes = array();
  26.         if ($limit > 60) $limit = 60;
  27.         for($m = 0; $m <= $limit; $m++)
  28.         {
  29.             $minute = str_pad($m, 2, '0', STR_PAD_LEFT);
  30.             $minutes[$minute] = $minute;
  31.         }
  32.         return $minutes;
  33.     }
  34. }
  35.  
  36. // ------------------------------------------------------------------------
  37.  
  38. if ( ! function_exists('list_hours'))
  39. {
  40.     /**
  41.      * List 24 hours with optional limit
  42.      *
  43.      * @access  public
  44.      * @param   integer
  45.      * @return  array
  46.      */
  47.     function list_hours($limit = 24)
  48.     {
  49.         $hours = array();
  50.         if ($limit > 24) $limit = 24;
  51.         for($h = 1; $h <= $limit; $h++)
  52.         {
  53.             $hour = str_pad($h, 2, '0', STR_PAD_LEFT);
  54.             $hours[$hour] = $hour;
  55.         }
  56.         return $hours;
  57.     }
  58. }
  59.  
  60. // ------------------------------------------------------------------------
  61.  
  62. if ( ! function_exists('list_days'))
  63. {
  64.     /**
  65.      * List days with optional limit
  66.      *
  67.      * @access  public
  68.      * @param   integer
  69.      * @return  array
  70.      */
  71.     function list_days($limit = 31)
  72.     {
  73.         $days = array();
  74.         if ($limit > 31) $limit = 31;
  75.         for($d = 1; $d <= $limit; $d++)
  76.         {
  77.             $day = str_pad($d, 2, '0', STR_PAD_LEFT);
  78.             $days[$day] = $day;
  79.         }
  80.         return $days;
  81.     }
  82. }
  83.  
  84. // ------------------------------------------------------------------------
  85.  
  86. if ( ! function_exists('list_months'))
  87. {
  88.     function list_months($limit = 12)
  89.     {
  90.         /**
  91.          * List months with optional limit
  92.          *
  93.          * @access  public
  94.          * @param   void
  95.          * @return  array
  96.          */
  97.         $CI =& get_instance();
  98.         $CI->load->helper('language');
  99.         // As you can see below, I use my own I18n library
  100.         // You can use the language you want or you can
  101.         // simple add a new parameter to this function to
  102.         // load your own language.
  103.         $CI->lang->load('calendar', I18n::get_current('folder'));
  104.         $months = array(
  105.             '01' => lang('cal_jan'),
  106.             '02' => lang('cal_feb'),
  107.             '03' => lang('cal_mar'),
  108.             '04' => lang('cal_apr'),
  109.             '05' => lang('cal_may'),
  110.             '06' => lang('cal_jun'),
  111.             '07' => lang('cal_jul'),
  112.             '08' => lang('cal_aug'),
  113.             '09' => lang('cal_sep'),
  114.             '10' => lang('cal_oct'),
  115.             '11' => lang('cal_nov'),
  116.             '12' => lang('cal_dec')
  117.         );
  118.         if ($limit > 12) $limit = 12;
  119.         return array_slice($months, 0, $limit, TRUE);
  120.     }
  121. }
  122.  
  123. // ------------------------------------------------------------------------
  124.  
  125. if ( ! function_exists('list_years'))
  126. {
  127.     /**
  128.      * List years with optional difference and quantity
  129.      *
  130.      * @access  public
  131.      * @param   integer
  132.      * @param   integer
  133.      * @return  array
  134.      */
  135.     function list_years($diff = 0, $qty = 100)
  136.     {
  137.         $years = array();
  138.         for ($y = date('Y') - $diff; $y >= date('Y') - $qty; $y--)
  139.         {
  140.             $years[$y] = $y;
  141.         }
  142.         return $years;
  143.     }
  144. }
  145.  
  146. /* End of file time_helper.php */
  147. /* Location: ./application/helpers/time_helper.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement