You Are Here : /var/www/virtual/tvarditsa.org/classes/ |
Current File : /var/www/virtual/tvarditsa.org/classes/datetime.class.php |
<?php // _$_ datetime.class.php _ datetime _ version 1.2 _ valio _$_ // if (!defined("_DATETIME_CLASS_")) { define("_DATETIME_CLASS_", 1); // --- Configuration --- // $DateTime_Configuration = array ( "bg" => array ( "months" => array ( 1 => "Януари", 2 => "Февруари", 3 => "Март", 4 => "Април", 5 => "Май", 6 => "Юни", 7 => "Юли", 8 => "Август", 9 => "Септември", 10 => "Октомври", 11 => "Ноември", 12 => "Декември" ), "weeks" => array ( 0 => "Неделя", 1 => "Понеделник", 2 => "Вторник", 3 => "Сряда", 4 => "Четвъртък", 5 => "Петък", 6 => "Събота", ), ), "en" => array ( "months" => 0, "weeks" => 0, ) ); // -- Class Definition -- // class DateTime { // ---- Private Vars ---- // var $_cfg; var $_timestamp; var $_sec; var $_min; var $_hour; var $_day; var $_month; var $_year; // ---- Constructor ---- // function DateTime ($lang = "bg", $cfg = null) { $this->__construct($lang, $cfg); } function __construct($lang = "bg", $cfg = null) { GLOBAL $DateTime_Configuration; if ($cfg == null) { if (isset($DateTime_Configuration[$lang])) { $this->_cfg = $DateTime_Configuration[$lang]; } else { $this->_cfg = $DateTime_Configuration[$lang]; } } else { $this->_cfg = $cfg; } } // --- Public Methods --- // function set_now () { list( $usec, $this->_timestamp ) = explode( " ", microtime() ); $now = getdate ( $this->_timestamp ); $this->_sec = $now["seconds"] + $usec; $this->_min = $now["minutes"]; $this->_hour = $now["hours"]; $this->_day = $now["mday"]; $this->_month = $now["mon"]; $this->_year = $now["year"]; } function set_date ($day, $month, $year) { $this->_sec = 0; $this->_min = 0; $this->_hour = 0; $this->_day = $day; $this->_month = $month; $this->_year = $year; if (($year >= 1970) && ($year <= 2038)) { $this->_timestamp = mktime (0, 0, 0, $month, $day, $year); } else { $this->_timestamp = -1; } } function get_date () { $r['sec'] = $this->_sec; $r['min'] = $this->_min; $r['hour'] = $this->_hour; $r['day'] = $this->_day; $r['month'] = $this->_month; $r['year'] = $this->_year; $r['ts'] = $this->_timestamp; return $r; } function set_time ($sec, $min, $hour) { $this->_sec = $sec; $this->_min = $min; $this->_hour = $hour; } function set_datetime ($sec, $min, $hour, $day, $month, $year) { $this->_sec = $sec; $this->_min = $min; $this->_hour = $hour; $this->_day = $day; $this->_month = $month; $this->_year = $year; if (($year >= 1970) && ($year <= 2038)) { $this->_timestamp = mktime (0, 0, 0, $month, $day, $year); } else { $this->_timestamp = -1; } } function get_datetime () { $ret = array(); $ret[0] = $this->_sec; $ret[1] = (int)$this->_min; $ret[2] = (int)$this->_hour; $ret[3] = (int)$this->_day; $ret[4] = (int)$this->_month; $ret[5] = (int)$this->_year; } function diff ($datetime_object) { $class_type = get_class ( $datetime_object ); $this_class_type = get_class ( $this ); if ($class_type != $this_class_type) return -1; if (($this->_timestamp > 0) && ($datetime_object->_timestamp > 0)) { $ts1 = $this->_timestamp - ((int)$this->_sec) + $this->_sec; $ts2 = $datetime_object->_timestamp - ((int)$datetime_object->_sec) + $datetime_object->_sec; return $ts1 - $ts2; } else return -2; } function month_name ($month) { return $this->_cfg["months"][$month]; } function week_name ($week) { return $this->_cfg["weeks"][$week]; } function format ($format = "%Y-%m-%d %T") { $tr = array(); if ($this->_timestamp >= 0) { if (is_array($this->_cfg["months"])) { $tr['%M'] = $this->_cfg["months"][date ("n", $this->_timestamp)]; } else { $tr['%M'] = '%b'; } $tr['%b'] = substr ($tr['%M'], 0, 3); if (is_array($this->_cfg["weeks"])) { $tr['%W'] = $this->_cfg["weeks"][date ("w", $this->_timestamp)]; } else { $tr['%W'] = '%A'; } $tr['%a'] = substr ($tr['%W'], 0, 3); $tr['%D'] = ''; //todo $tr['%X'] = '%G'; $tr['%x'] = '%G'; $tr['%c'] = '%m'; $tr['%k'] = '%H'; $tr['%l'] = '%I'; $tr['%i'] = '%M'; $tr['%r'] = '%I:%M:%S %p'; $tr['%T'] = '%H:%M:%S'; $tr['%s'] = '%S'; $tr['%%'] = '%'; $format = strtr($format, $tr); return strftime ($format, $this->_timestamp); } else { if (is_array($this->_cfg["months"])) { $tr['%M'] = $this->_cfg["months"][$this->_month]; } else { $tr['%M'] = date ("F", mktime (0,0,0,$this->_month,1,2002)); } $tr['%b'] = substr ($tr['%M'], 0, 3); // todo weeks if ($this->_month < 10) $tr['%m'] = '0'.(int)$this->_month; else $tr['%m'] = $this->_month; $tr['%c'] = $this->_month; $tr['%Y'] = $this->_year; $tr['%y'] = $this->_year % 100; $tr['%d'] = $this->_day; $tr['%e'] = $this->_day; $tr['%k'] = $this->_hour; if ($this->_hour < 10) $tr['%H'] = '0'.(int)$this->_hour; else $tr['%H'] = $this->_hour; $h2 = $this->_hour % 12; if ($h2 < 10) $tr['%h'] = '0'.(int)$h2; else $tr['%h'] = $h2; $tr['%I'] = $tr['%h']; if ($this->_min < 10) $tr['%i'] = '0'.(int)$this->_min; else $tr['%i'] = $this->_min; if ($this->_sec < 10) $tr['%S'] = '0'.(int)$this->_sec; else $tr['%S'] = $this->_sec; $tr['%s'] = $tr['%S']; $tr['%T'] = $tr['%H'].':'.$tr['%i'].':'.$tr['%S']; $tr['%r'] = $tr['%h'].':'.$tr['%i'].':'.$tr['%S'].' '.((int)($this->_hour / 12) ? 'A' : 'P').'M'; $tr['%%'] = '%'; $format = strtr($format, $tr); return $format; } } function from_string ($str) { $this->_timestamp = strtotime ( $str ); if ($this->_timestamp != -1) { $now = getdate ( $this->_timestamp ); $this->_sec = $now["seconds"] + $usec; $this->_min = $now["minutes"]; $this->_hour = $now["hours"]; $this->_day = $now["mday"]; $this->_month = $now["mon"]; $this->_year = $now["year"]; } else { if (preg_match ("/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/", $str, $match)) { $this->_timestamp = -1; $this->_sec = $match[6]; $this->_min = $match[5]; $this->_hour = $match[4]; $this->_day = $match[3]; $this->_month = $match[2]; $this->_year = $match[1]; } else { // todo: more types } } } // -- Private Methods -- // // ----- Destructor ----- // function __destruct() { } } } // END DATETIME_CLASS ?>