1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32:
33: class Mage_Core_Model_Date
34: {
35: 36: 37: 38: 39:
40: private $_offset = 0;
41:
42: 43: 44: 45: 46:
47: private $_systemOffset = 0;
48:
49: 50: 51: 52:
53: public function __construct()
54: {
55: $this->_offset = $this->calculateOffset($this->_getConfigTimezone());
56: $this->_systemOffset = $this->calculateOffset();
57: }
58:
59: 60: 61: 62: 63:
64: protected function _getConfigTimezone()
65: {
66: return Mage::app()->getStore()->getConfig('general/locale/timezone');
67: }
68:
69: 70: 71: 72: 73: 74:
75: public function calculateOffset($timezone = null)
76: {
77: $result = true;
78: $offset = 0;
79:
80: if (!is_null($timezone)){
81: $oldzone = @date_default_timezone_get();
82: $result = date_default_timezone_set($timezone);
83: }
84:
85: if ($result === true) {
86: $offset = gmmktime(0, 0, 0, 1, 2, 1970) - mktime(0, 0, 0, 1, 2, 1970);
87: }
88:
89: if (!is_null($timezone)){
90: date_default_timezone_set($oldzone);
91: }
92:
93: return $offset;
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public function gmtDate($format = null, $input = null)
104: {
105: if (is_null($format)) {
106: $format = 'Y-m-d H:i:s';
107: }
108:
109: $date = $this->gmtTimestamp($input);
110:
111: if ($date === false) {
112: return false;
113: }
114:
115: $result = date($format, $date);
116: return $result;
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126:
127: public function date($format = null, $input = null)
128: {
129: if (is_null($format)) {
130: $format = 'Y-m-d H:i:s';
131: }
132:
133: $result = date($format, $this->timestamp($input));
134: return $result;
135: }
136:
137: 138: 139: 140: 141: 142:
143: public function gmtTimestamp($input = null)
144: {
145: if (is_null($input)) {
146: return gmdate('U');
147: } else if (is_numeric($input)) {
148: $result = $input;
149: } else {
150: $result = strtotime($input);
151: }
152:
153: if ($result === false) {
154:
155: return false;
156: }
157:
158: $date = Mage::app()->getLocale()->date($result);
159: $timestamp = $date->get(Zend_Date::TIMESTAMP) - $date->get(Zend_Date::TIMEZONE_SECS);
160:
161: unset($date);
162: return $timestamp;
163:
164: }
165:
166: 167: 168: 169: 170: 171: 172:
173: public function timestamp($input = null)
174: {
175: if (is_null($input)) {
176: $result = $this->gmtTimestamp();
177: } else if (is_numeric($input)) {
178: $result = $input;
179: } else {
180: $result = strtotime($input);
181: }
182:
183: $date = Mage::app()->getLocale()->date($result);
184: $timestamp = $date->get(Zend_Date::TIMESTAMP) + $date->get(Zend_Date::TIMEZONE_SECS);
185:
186: unset($date);
187: return $timestamp;
188: }
189:
190: 191: 192: 193: 194: 195:
196: public function getGmtOffset($type = 'seconds')
197: {
198: $result = $this->_offset;
199: switch ($type) {
200: case 'seconds':
201: default:
202: break;
203:
204: case 'minutes':
205: $result = $result / 60;
206: break;
207:
208: case 'hours':
209: $result = $result / 60 / 60;
210: break;
211: }
212: return $result;
213: }
214:
215: 216: 217:
218: public function checkDateTime($year, $month, $day, $hour = 0, $minute = 0, $second = 0)
219: {
220: if (!checkdate($month, $day, $year)) {
221: return false;
222: }
223: foreach (array('hour' => 23, 'minute' => 59, 'second' => 59) as $var => $maxValue) {
224: $value = (int)$$var;
225: if (($value < 0) || ($value > $maxValue)) {
226: return false;
227: }
228: }
229: return true;
230: }
231:
232: 233: 234:
235: public function parseDateTime($dateTimeString, $dateTimeFormat)
236: {
237:
238: $isSupportedFormatFound = false;
239:
240: $formats = array(
241:
242: '%m/%d/%y %I:%M' => array(
243: '/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})/',
244: array('y' => 3, 'm' => 1, 'd' => 2, 'h' => 4, 'i' => 5)
245: ),
246: 'm/d/y h:i' => array(
247: '/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})/',
248: array('y' => 3, 'm' => 1, 'd' => 2, 'h' => 4, 'i' => 5)
249: ),
250: '%m/%d/%y' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2)),
251: 'm/d/y' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2)),
252: );
253:
254: foreach ($formats as $supportedFormat => $regRule) {
255: if (false !== strpos($dateTimeFormat, $supportedFormat, 0)) {
256: $isSupportedFormatFound = true;
257: break;
258: }
259: }
260: if (!$isSupportedFormatFound) {
261: Mage::throwException(Mage::helper('core')->__('Date/time format "%s" is not supported.', $dateTimeFormat));
262: }
263:
264:
265: $regex = array_shift($regRule);
266: $mask = array_shift($regRule);
267: if (!preg_match($regex, $dateTimeString, $matches)) {
268: Mage::throwException(Mage::helper('core')->__('Specified date/time "%1$s" do not match format "%2$s".', $dateTimeString, $dateTimeFormat));
269: }
270:
271:
272: $result = array();
273: foreach (array('y', 'm', 'd', 'h', 'i', 's') as $key) {
274: $value = 0;
275: if (isset($mask[$key]) && isset($matches[$mask[$key]])) {
276: $value = (int)$matches[$mask[$key]];
277: }
278: $result[] = $value;
279: }
280:
281:
282: if ($result[0] < 100) {
283: $result[0] = 2000 + $result[0];
284: }
285:
286: return $result;
287: }
288: }
289: