<?php
/**
* DS Thaidate v2.0.0
*
* @name Thaidate Class
* @version 2.0.0
* @author Narong Rammanee, <hide@address.com>
* @copyright Copyright (c) 2010, Narong Rammanee
* @license No Licence Free 2010
*/
class Thaidate {
private $_day;
private $_month;
private $_year;
private $_hour;
private $_minute;
private $_second;
private $_th_day;
private $_th_month;
private $_th_year;
private $_th_date;
private $_th_time;
private $_format;
private $_tmp;
private $full_day = array('Sun'=>'à¸à¸²à¸à¸´à¸à¸¢à¹', 'Mon'=>'à¸à¸±à¸à¸à¸£à¹', 'Tue'=>'à¸à¸±à¸à¸à¸²à¸£', 'Wed'=>'à¸à¸¸à¸',
'Thu'=>'à¸à¸¤à¸«à¸±à¸ªà¸à¸à¸µ', 'Fri'=>'ศุà¸à¸£à¹', 'Sat'=>'à¹à¸ªà¸²à¸£à¹'
);
private $quick_day = array('Sun'=>'à¸à¸².', 'Mon'=>'à¸.', 'Tue'=>'à¸.', 'Wed'=>'à¸.',
'Thu'=>'à¸à¸¤.', 'Fri'=>'ศ.', 'Sat'=>'ส.'
);
private $full_month = array('01'=>'มà¸à¸£à¸²à¸à¸¡', '02'=>'à¸à¸¸à¸¡à¸ าà¸à¸±à¸à¸à¹', '03'=>'มีà¸à¸²à¸à¸¡', '04'=>'à¹à¸¡à¸©à¸²à¸¢à¸',
'05'=>'à¸à¸¤à¸©à¸ าà¸à¸¡', '06'=>'มิà¸à¸¸à¸à¸²à¸¢à¸', '07'=>'à¸à¸£à¸à¸à¸²à¸à¸¡', '08'=>'สิà¸à¸«à¸²à¸à¸¡',
'09'=>'à¸à¸±à¸à¸¢à¸²à¸¢à¸', '10'=>'à¸à¸¸à¸¥à¸²à¸à¸¡', '11'=>'à¸à¸¤à¸¨à¸à¸´à¸à¸²à¸¢à¸', '12'=>'à¸à¸±à¸à¸§à¸²à¸à¸¡'
);
private $quick_month = array('01'=>'ม.à¸.', '02'=>'à¸.à¸.', '03'=>'มี.à¸.', '04'=>'à¹à¸¡.ย.',
'05'=>'à¸.à¸.', '06'=>'มิ.ย.', '07'=>'à¸.à¸.', '08'=>'ส.à¸.',
'09'=>'à¸.ย.', '10'=>'à¸.à¸.', '11'=>'à¸.ย.', '12'=>'à¸.à¸.'
);
private $_th_number = array('0'=>'à¹', '1'=>'à¹', '2'=>'à¹', '3'=>'à¹','4'=>'à¹',
'5'=>'à¹', '6'=>'à¹', '7'=>'à¹', '8'=>'à¹', '9'=>'à¹'
);
/**
* Initialization
*
* @param string $date
* @param string $format
*/
public function __construct($date, $format) {
$this->_day = substr($date, 8, -11);
$this->_month = substr($date, 5, -14);
$this->_year = substr($date, 0, -17);
$this->_hour = substr($date, 13, -6);
$this->_minute = substr($date, 16, -3);
$this->_second = substr($date, 19);
$this->_format = $format;
}
/**
* Get date time
*/
public function getDateTime() {
if(!$this->_th_date = self::convertDate())
throw new Exception('Error obtaining date!');
return $this->_th_date;
}
/**
* Convert datetime to thai date time format
*/
private function convertDate() {
$this->_th_day = $this->full_day[date("D", mktime(0, 0, 0, $this->_month, $this->_day, $this->_year))];
$this->_th_month = ($this->_format == 'E8' || $this->_format == 'E10')? $this->quick_month[$this->_month] : $this->full_month[$this->_month];
$this->_th_year = $this->_year + 543;
$this->_th_time = ($this->_format != 'E4') ? $this->_hour.':'.$this->_minute.' à¸.'
: date("g:i A", mktime($this->_hour, $this->_minute, 0, 0, 0, 0));
switch($this->_format) {
case 'E1' : $this->_th_date = $this->_day.'/'.(int) $this->_month.'/'.substr($this->_th_year, -2); break;
case 'E2' : $this->_th_date = $this->_day.'/'.(int) $this->_month.'/'.$this->_th_year; break;
case 'E3' :
case 'E4' : $this->_th_date = $this->_day.'/'.(int) $this->_month.'/'.$this->_th_year.' '.$this->_th_time; break;
case 'E5' : $this->_th_date = self::thaiNumber($this->_day).'/'.self::thaiNumber($this->_month).'/'.self::thaiNumber(substr($this->_th_year, -2)); break;
case 'E6' : $this->_th_date = self::thaiNumber($this->_day).'/'.self::thaiNumber($this->_month).'/'.self::thaiNumber($this->_th_year); break;
case 'E7' : $this->_th_date = self::thaiNumber($this->_day).'/'.self::thaiNumber($this->_month).'/'.self::thaiNumber($this->_th_year).' '.self::thaiNumber($this->_hour).':'.self::thaiNumber($this->_minute).' à¸.'; break;
case 'E8' : $this->_th_date = $this->_day.' '.$this->_th_month.' '.$this->_th_year; break;
case 'E9' : $this->_th_date = $this->_day.' '.$this->_th_month.' '.$this->_th_year; break;
case 'E10' : $this->_th_date = self::thaiNumber($this->_day).' '.$this->_th_month.' '.self::thaiNumber($this->_th_year); break;
case 'E11' : $this->_th_date = self::thaiNumber($this->_day).' '.$this->_th_month.' '.self::thaiNumber($this->_th_year); break;
case 'E12' : $this->_th_date = $this->_day.' '.date("M", mktime(0, 0, 0, $this->_month, 0, 0)).' '.substr($this->_th_year, -2); break;
case 'E13' : $this->_th_date = $this->_day.' '.date("M", mktime(0, 0, 0, $this->_month, 0, 0)).' '.$this->_th_year; break;
case 'F1' : $this->_th_date = 'วัà¸'.$this->_th_day.' à¸à¸µà¹ '. $this->_day.' à¹à¸à¸·à¸à¸'.$this->_th_month.' à¸.ศ. '.$this->_th_year; break;
case 'F2' : $this->_th_date = 'วัà¸'.$this->_th_day.' à¸à¸µà¹ '. $this->_day.' à¹à¸à¸·à¸à¸'.$this->_th_month.' à¸.ศ. '.$this->_th_year.' à¹à¸§à¸¥à¸² '.$this->_th_time; break;
}
return $this->_th_date;
}
private function thaiNumber($ar_number) {
unset($this->_tmp);
$ar_number = str_split($ar_number);
foreach($ar_number as $value)
$this->_tmp .= $this->_th_number[$value];
return $this->_tmp;
}
}