Advertisement
n0tmE

Thai date utility

Aug 10th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.71 KB | None | 0 0
  1. <?php
  2. //=======================================================================
  3. // Description: Date & Time utility class
  4. // Author:  Sirichai Meemutha (jjoi@gayji.net)
  5. // Ver:     $Id: class.DateUtil.php v1.02
  6. //
  7. // License: This code is released under GPL
  8. // Copyright (C) 2005 JF Systems
  9. //========================================================================
  10.  
  11. class DateUtil {
  12.     public $ts;
  13.    
  14.     public static $arrMonthName = array(1 => 'มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน','กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม');
  15.     public static $arrShortMonthName = array(1 => 'ม.ค.','ก.พ.','มี.ค.','เม.ย.','พ.ค.','มิ.ย.','ก.ค.','ส.ค.','ก.ย.','ต.ค.','พ.ย.','ธ.ค.');
  16.     public static $arrDoW = array('อาทิตย์','จันทร์','อังคาร','พุธ','พฤหัสบดี','ศุกร์','เสาร์');
  17.     public static $arrShortDoW = array('อา','จ','อ','พ','พฤ','ศ','ส');
  18.  
  19.     public static $arrMonthNameEN = array(1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  20.     public static $arrShortMonthNameEN = array(1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  21.     public static $arrDoWEN = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  22.     public static $arrShortDoWEN = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
  23.    
  24.    
  25.     public function __construct($ts = NULL) {
  26.         if (is_null($ts))
  27.             $ts = time();
  28.            
  29.         $this -> ts = $ts;
  30.     }
  31.    
  32.     public function duration($ts) {
  33.         $distance = $this->ts - $ts;
  34.         list($Y, $n, $j) = self::dateParts('Ynj', $ts);
  35.         $days = intval(($this->ts - $ts) / self::SECS_PER_DAY);
  36.    
  37.         // new timestamp, substract yer
  38.         $years = intval($days / 365);      
  39.         $ts = mktime(0, 0, 0, $n, $j, $Y + $years);
  40.         list($Y, $n, $j) = self::dateParts('Ynj', $ts);    
  41.         $days = intval(($this->ts - $ts) / self::SECS_PER_DAY);
  42.  
  43.         $months = intval($days / 30);
  44.         $ts = mktime(0, 0, 0, $n + $months, $j, $Y);
  45.         list($Y, $n, $j) = self::dateParts('Ynj', $ts);
  46.         $days = intval(($this->ts - $ts) / self::SECS_PER_DAY);
  47.    
  48.         return array(
  49.             'year'=>$years,
  50.             'month'=>$months,
  51.             'day'=>$days,
  52.         );
  53.     }
  54.    
  55.     public function GetFDOM($ts = NULL) {
  56.         if (!is_numeric($ts))
  57.             $ts = $this -> ts;
  58.            
  59.         list($cal_n, $cal_Y) = self::DateParts('nY', $ts); 
  60.         return mktime(0, 0, 0, $cal_n, 1, $cal_Y);
  61.     }
  62.    
  63.     public  function GetLDOM($ts = NULL) {
  64.         if (!is_numeric($ts))
  65.             $ts = $this -> ts;
  66.            
  67.         list($cal_n, $cal_Y) = self::DateParts('nY', $ts); 
  68.         return mktime(0, 0, 0, $cal_n + 1, 0, $cal_Y);
  69.     }
  70.    
  71.     public function GetMaxJ($ts = NULL) {          
  72.         return intval(date('j', $this -> GetLDOM($ts)));
  73.     }
  74.    
  75.     /**
  76.      * หาค่า timestamp ของวันปัจจุบัน ณ เที่ยงคืน
  77.      *
  78.      * @return int
  79.      */
  80.     public function Today() {
  81.         list($j, $n, $Y) = self::DateParts('jnY', time());
  82.         return (mktime(0, 0, 0, $n, $j, $Y));
  83.     }
  84.    
  85.     // static functions
  86.     public static function th_date($format,$t=NULL)
  87.     {      
  88.         if (!is_numeric($t))
  89.             return NULL;
  90.  
  91.         $output = '';
  92.  
  93.         $arrFormat = explode("\r\n",chunk_split($format,1));
  94.         foreach ($arrFormat as $key => $val)
  95.         {
  96.             if(preg_match('/[aABdgGhHiIjLmnOrsStTUwWzZ]/',$val))
  97.             {
  98.                 $output .= date($val,$t);
  99.             }
  100.             else
  101.             {
  102.                 switch($val)
  103.                 {
  104.                     case 'D':       // Day of Week, short name
  105.                         $output .= self::$arrShortDoW[date('w',$t)];
  106.                         break;
  107.                     case 'F':       // Month, full name
  108.                         $output .= self::$arrMonthName[date('n',$t)];
  109.                         break;
  110.                     case 'l':           // Day of Week , long name
  111.                         $output .= self::$arrDoW[date('w',$t)];
  112.                         break;
  113.                     case 'M':       // Month, short name
  114.                         $output .= self::$arrShortMonthName[date('n',$t)];
  115.                         break;
  116.                     case 'Y':       // 4 digit Year
  117.                         $output .= date('Y',$t) + self::AD2BE;
  118.                         break;
  119.                     case 'y':       // 2 digit Year
  120.                         $output .= (date('y',$t) + 43) % 100;
  121.                         break;
  122.                     default:
  123.                             $output .= $val;
  124.                 }
  125.             }
  126.         }
  127.         return $output;
  128.     }
  129.    
  130.     // usage ex: list($Y,$m,$j) = DateUtil::DateParts("Ymj",$time_var);
  131.     public static function dateParts($dateFormat,$t = NULL)
  132.     {
  133. //      if (!$t) $t = time();
  134.         $arr_keys = preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY);
  135.         while(list($key,$val) = each($arr_keys))
  136.         {
  137.             $value = is_null($t)?NULL:date($val,$t);
  138.             $arr_output[$val] = $value;
  139.             $arr_output[$key] = $value;
  140.         }
  141.         return $arr_output;
  142.     }
  143.    
  144.     /**
  145.      * แปลงค่า date/time string ให้เป็น timestamp
  146.      *
  147.      * @param string $str
  148.      * @param int $BEYear
  149.      * @return int
  150.      */
  151.     public static function Parse($str, $BEYear = true) {
  152.         preg_match('/(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*(\d{1,2})?\:?(\d{1,2})?\:?(\d{1,2})?/', $str, $matches);
  153.         $yearOffset = $BEYear?DateUtil::AD2BE:0;
  154.         if (DateUtil::DATEFORMAT == 'jnY') {
  155.             $dIndex = 1;
  156.             $mIndex = 2;
  157.             $yIndex = 3;
  158.         }
  159.         else {
  160.             $dIndex = 2;
  161.             $mIndex = 1;
  162.             $yIndex = 3;
  163.         }      
  164.  
  165.         if (count($matches) == 4) {
  166.             return mktime(0, 0, 0, $matches[$mIndex], $matches[$dIndex], $matches[$yIndex] - $yearOffset);
  167.         }
  168.         else if(count($matches) >= 6) {
  169.             $hh = $matches[4];
  170.             $mm = $matches[5];
  171.             $ss = $matches[6]?$matches[6]:0;
  172.             return mktime($hh, $mm, $ss, $matches[$mIndex], $matches[$dIndex], $matches[$yIndex] - $yearOffset);
  173.         }
  174.         else
  175.             return NULL;
  176.     }
  177.    
  178.     /**
  179.      * Parse date string ใน ISO 8601 format
  180.      * @param string $str ค่าของ date string
  181.      * @return int
  182.      */
  183.     public static function ParseSQLDate($str) {
  184.         if(is_null($str))
  185.             return NULL;
  186.         $dt = @strtotime($str);
  187.  
  188.         if ($dt === -1 || $dt === false)
  189.             return NULL;
  190.         else
  191.             return $dt;
  192.     }
  193.    
  194.     /**
  195.      * get Day ในแต่ละ Week
  196.      * @reture  boolean
  197.      */
  198.     public static function getSunToMonDay(){
  199.         if((date(self::NUMERIC_FORMAT_DAY) == self::SUN_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::MON_NUMERIC))
  200.             return true;
  201.         else
  202.             return false;
  203.        
  204.     }
  205.     /**
  206.      * get Day ในแต่ละ Week
  207.      * @reture  boolean
  208.      */
  209.     public static function getFriToTue(){
  210.         if((date(self::NUMERIC_FORMAT_DAY) == self::FRI_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::SAT_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::SUN_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::MON_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::TUE_NUMERIC))
  211.             return true;
  212.         else
  213.             return false;
  214.        
  215.     }
  216.    
  217.     /**
  218.      * get Day ในแต่ละ Week
  219.      * @reture  boolean
  220.      */
  221.     public static function getFriToSun(){
  222.         if((date(self::NUMERIC_FORMAT_DAY) == self::FRI_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::SAT_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::SUN_NUMERIC))
  223.             return true;
  224.         else
  225.             return false;
  226.        
  227.     }
  228.    
  229.     /**
  230.     * get Day ในแต่ละ Week
  231.     * @reture  boolean
  232.     */
  233.     public static function getFriToSAT(){
  234.         if((date(self::NUMERIC_FORMAT_DAY) == self::FRI_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::SAT_NUMERIC))
  235.             return true;
  236.         else
  237.             return false;
  238.    
  239.     }
  240.    
  241.     /**
  242.      * get Day ในแต่ละ Week
  243.      * @reture  boolean
  244.      */
  245.     public static function getWedToThu(){
  246.         if((date(self::NUMERIC_FORMAT_DAY) == self::WED_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::THU_NUMERIC))
  247.             return true;
  248.         else
  249.             return false;
  250.        
  251.     }
  252.    
  253.     public static function getTime(){  
  254.         if(strtotime(date('H:i:s',time())) >= strtotime('00:00:00') && strtotime(date('H:i:s',time())) <= strtotime('12:00:00') ){
  255.             return self::HALF_DAY_BREAKFAST;
  256.         }
  257.         return self::HALF_DAY_AFTERNOON;
  258.     }
  259.     // constants
  260.     const AD2BE = 543;
  261.     const SECS_PER_DAY = 86400;
  262.     const DATEFORMAT = 'jnY';
  263.    
  264.     const SD_FMT_FORM = 'j/n/Y';
  265.     const SDT_FMT_FORM = 'j/n/Y H:i:s';
  266.    
  267.     const SD_FMT_TH = 'j M y';
  268.     const LD_FMT_TH = 'j F Y';
  269.     const SDT_FMT_TH = 'j M y, H:i น.';
  270.     const LDT_FMT_TH = 'j F Y, H:i น.';
  271.     const ST_FMT_TH = 'H:i น.';
  272.     const ST_FMT_TWOPOSITION_FORM = 'H:i'; 
  273.    
  274.     const SQL_DT_FMT = 'Y-m-d H:i:s';
  275.     const SQL_D_FMT = 'Y-m-d';
  276.    
  277.     const NUMERIC_FORMAT_DAY = 'N';
  278.    
  279.     const MON_NUMERIC =1;
  280.     const TUE_NUMERIC =2;
  281.     const WED_NUMERIC =3;
  282.     const THU_NUMERIC =4;
  283.     const FRI_NUMERIC =5;
  284.     const SAT_NUMERIC =6;
  285.     const SUN_NUMERIC =7;
  286.    
  287.     const HALF_DAY_BREAKFAST = 0;
  288.     const HALF_DAY_AFTERNOON = 1;
  289.    
  290.     public static $arrDayNumeric = array(
  291.         self::MON_NUMERIC => 1,
  292.         self::TUE_NUMERIC => 2,
  293.         self::WED_NUMERIC => 3,
  294.         self::THU_NUMERIC => 4,
  295.         self::FRI_NUMERIC => 5,
  296.         self::SAT_NUMERIC => 6,
  297.         self::SUN_NUMERIC => 7,
  298.     );
  299. }
  300. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement