Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //=======================================================================
- // Description: Date & Time utility class
- // Author: Sirichai Meemutha (jjoi@gayji.net)
- // Ver: $Id: class.DateUtil.php v1.02
- //
- // License: This code is released under GPL
- // Copyright (C) 2005 JF Systems
- //========================================================================
- class DateUtil {
- public $ts;
- public static $arrMonthName = array(1 => 'มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน','กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม');
- public static $arrShortMonthName = array(1 => 'ม.ค.','ก.พ.','มี.ค.','เม.ย.','พ.ค.','มิ.ย.','ก.ค.','ส.ค.','ก.ย.','ต.ค.','พ.ย.','ธ.ค.');
- public static $arrDoW = array('อาทิตย์','จันทร์','อังคาร','พุธ','พฤหัสบดี','ศุกร์','เสาร์');
- public static $arrShortDoW = array('อา','จ','อ','พ','พฤ','ศ','ส');
- public static $arrMonthNameEN = array(1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
- public static $arrShortMonthNameEN = array(1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
- public static $arrDoWEN = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
- public static $arrShortDoWEN = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
- public function __construct($ts = NULL) {
- if (is_null($ts))
- $ts = time();
- $this -> ts = $ts;
- }
- public function duration($ts) {
- $distance = $this->ts - $ts;
- list($Y, $n, $j) = self::dateParts('Ynj', $ts);
- $days = intval(($this->ts - $ts) / self::SECS_PER_DAY);
- // new timestamp, substract yer
- $years = intval($days / 365);
- $ts = mktime(0, 0, 0, $n, $j, $Y + $years);
- list($Y, $n, $j) = self::dateParts('Ynj', $ts);
- $days = intval(($this->ts - $ts) / self::SECS_PER_DAY);
- $months = intval($days / 30);
- $ts = mktime(0, 0, 0, $n + $months, $j, $Y);
- list($Y, $n, $j) = self::dateParts('Ynj', $ts);
- $days = intval(($this->ts - $ts) / self::SECS_PER_DAY);
- return array(
- 'year'=>$years,
- 'month'=>$months,
- 'day'=>$days,
- );
- }
- public function GetFDOM($ts = NULL) {
- if (!is_numeric($ts))
- $ts = $this -> ts;
- list($cal_n, $cal_Y) = self::DateParts('nY', $ts);
- return mktime(0, 0, 0, $cal_n, 1, $cal_Y);
- }
- public function GetLDOM($ts = NULL) {
- if (!is_numeric($ts))
- $ts = $this -> ts;
- list($cal_n, $cal_Y) = self::DateParts('nY', $ts);
- return mktime(0, 0, 0, $cal_n + 1, 0, $cal_Y);
- }
- public function GetMaxJ($ts = NULL) {
- return intval(date('j', $this -> GetLDOM($ts)));
- }
- /**
- * หาค่า timestamp ของวันปัจจุบัน ณ เที่ยงคืน
- *
- * @return int
- */
- public function Today() {
- list($j, $n, $Y) = self::DateParts('jnY', time());
- return (mktime(0, 0, 0, $n, $j, $Y));
- }
- // static functions
- public static function th_date($format,$t=NULL)
- {
- if (!is_numeric($t))
- return NULL;
- $output = '';
- $arrFormat = explode("\r\n",chunk_split($format,1));
- foreach ($arrFormat as $key => $val)
- {
- if(preg_match('/[aABdgGhHiIjLmnOrsStTUwWzZ]/',$val))
- {
- $output .= date($val,$t);
- }
- else
- {
- switch($val)
- {
- case 'D': // Day of Week, short name
- $output .= self::$arrShortDoW[date('w',$t)];
- break;
- case 'F': // Month, full name
- $output .= self::$arrMonthName[date('n',$t)];
- break;
- case 'l': // Day of Week , long name
- $output .= self::$arrDoW[date('w',$t)];
- break;
- case 'M': // Month, short name
- $output .= self::$arrShortMonthName[date('n',$t)];
- break;
- case 'Y': // 4 digit Year
- $output .= date('Y',$t) + self::AD2BE;
- break;
- case 'y': // 2 digit Year
- $output .= (date('y',$t) + 43) % 100;
- break;
- default:
- $output .= $val;
- }
- }
- }
- return $output;
- }
- // usage ex: list($Y,$m,$j) = DateUtil::DateParts("Ymj",$time_var);
- public static function dateParts($dateFormat,$t = NULL)
- {
- // if (!$t) $t = time();
- $arr_keys = preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY);
- while(list($key,$val) = each($arr_keys))
- {
- $value = is_null($t)?NULL:date($val,$t);
- $arr_output[$val] = $value;
- $arr_output[$key] = $value;
- }
- return $arr_output;
- }
- /**
- * แปลงค่า date/time string ให้เป็น timestamp
- *
- * @param string $str
- * @param int $BEYear
- * @return int
- */
- public static function Parse($str, $BEYear = true) {
- preg_match('/(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*(\d{1,2})?\:?(\d{1,2})?\:?(\d{1,2})?/', $str, $matches);
- $yearOffset = $BEYear?DateUtil::AD2BE:0;
- if (DateUtil::DATEFORMAT == 'jnY') {
- $dIndex = 1;
- $mIndex = 2;
- $yIndex = 3;
- }
- else {
- $dIndex = 2;
- $mIndex = 1;
- $yIndex = 3;
- }
- if (count($matches) == 4) {
- return mktime(0, 0, 0, $matches[$mIndex], $matches[$dIndex], $matches[$yIndex] - $yearOffset);
- }
- else if(count($matches) >= 6) {
- $hh = $matches[4];
- $mm = $matches[5];
- $ss = $matches[6]?$matches[6]:0;
- return mktime($hh, $mm, $ss, $matches[$mIndex], $matches[$dIndex], $matches[$yIndex] - $yearOffset);
- }
- else
- return NULL;
- }
- /**
- * Parse date string ใน ISO 8601 format
- * @param string $str ค่าของ date string
- * @return int
- */
- public static function ParseSQLDate($str) {
- if(is_null($str))
- return NULL;
- $dt = @strtotime($str);
- if ($dt === -1 || $dt === false)
- return NULL;
- else
- return $dt;
- }
- /**
- * get Day ในแต่ละ Week
- * @reture boolean
- */
- public static function getSunToMonDay(){
- if((date(self::NUMERIC_FORMAT_DAY) == self::SUN_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::MON_NUMERIC))
- return true;
- else
- return false;
- }
- /**
- * get Day ในแต่ละ Week
- * @reture boolean
- */
- public static function getFriToTue(){
- 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))
- return true;
- else
- return false;
- }
- /**
- * get Day ในแต่ละ Week
- * @reture boolean
- */
- public static function getFriToSun(){
- 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))
- return true;
- else
- return false;
- }
- /**
- * get Day ในแต่ละ Week
- * @reture boolean
- */
- public static function getFriToSAT(){
- if((date(self::NUMERIC_FORMAT_DAY) == self::FRI_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::SAT_NUMERIC))
- return true;
- else
- return false;
- }
- /**
- * get Day ในแต่ละ Week
- * @reture boolean
- */
- public static function getWedToThu(){
- if((date(self::NUMERIC_FORMAT_DAY) == self::WED_NUMERIC) || (date(self::NUMERIC_FORMAT_DAY) == self::THU_NUMERIC))
- return true;
- else
- return false;
- }
- public static function getTime(){
- if(strtotime(date('H:i:s',time())) >= strtotime('00:00:00') && strtotime(date('H:i:s',time())) <= strtotime('12:00:00') ){
- return self::HALF_DAY_BREAKFAST;
- }
- return self::HALF_DAY_AFTERNOON;
- }
- // constants
- const AD2BE = 543;
- const SECS_PER_DAY = 86400;
- const DATEFORMAT = 'jnY';
- const SD_FMT_FORM = 'j/n/Y';
- const SDT_FMT_FORM = 'j/n/Y H:i:s';
- const SD_FMT_TH = 'j M y';
- const LD_FMT_TH = 'j F Y';
- const SDT_FMT_TH = 'j M y, H:i น.';
- const LDT_FMT_TH = 'j F Y, H:i น.';
- const ST_FMT_TH = 'H:i น.';
- const ST_FMT_TWOPOSITION_FORM = 'H:i';
- const SQL_DT_FMT = 'Y-m-d H:i:s';
- const SQL_D_FMT = 'Y-m-d';
- const NUMERIC_FORMAT_DAY = 'N';
- const MON_NUMERIC =1;
- const TUE_NUMERIC =2;
- const WED_NUMERIC =3;
- const THU_NUMERIC =4;
- const FRI_NUMERIC =5;
- const SAT_NUMERIC =6;
- const SUN_NUMERIC =7;
- const HALF_DAY_BREAKFAST = 0;
- const HALF_DAY_AFTERNOON = 1;
- public static $arrDayNumeric = array(
- self::MON_NUMERIC => 1,
- self::TUE_NUMERIC => 2,
- self::WED_NUMERIC => 3,
- self::THU_NUMERIC => 4,
- self::FRI_NUMERIC => 5,
- self::SAT_NUMERIC => 6,
- self::SUN_NUMERIC => 7,
- );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement