Mage_Core_Model_Date Class Reference

List of all members.

Public Member Functions

 __construct ()
 calculateOffset ($timezone=null)
 gmtDate ($format=null, $input=null)
 date ($format=null, $input=null)
 gmtTimestamp ($input=null)
 timestamp ($input=null)
 getGmtOffset ($type= 'seconds')
 checkDateTime ($year, $month, $day, $hour=0, $minute=0, $second=0)
 parseDateTime ($dateTimeString, $dateTimeFormat)

Protected Member Functions

 _getConfigTimezone ()


Detailed Description

Date conversion model

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 33 of file Date.php.


Constructor & Destructor Documentation

__construct (  ) 

Init offset

Definition at line 53 of file Date.php.

00054     {
00055         $this->_offset = $this->calculateOffset($this->_getConfigTimezone());
00056         $this->_systemOffset = $this->calculateOffset();
00057     }


Member Function Documentation

_getConfigTimezone (  )  [protected]

Gets the store config timezone

Returns:
string

Definition at line 64 of file Date.php.

00065     {
00066         return Mage::app()->getStore()->getConfig('general/locale/timezone');
00067     }

calculateOffset ( timezone = null  ) 

Definition at line 75 of file Date.php.

00076     {
00077         $result = true;
00078         $offset = 0;
00079 
00080         if (!is_null($timezone)){
00081             $oldzone = @date_default_timezone_get();
00082             $result = date_default_timezone_set($timezone);
00083         }
00084 
00085         if ($result === true) {
00086             $offset = gmmktime(0, 0, 0, 1, 2, 1970) - mktime(0, 0, 0, 1, 2, 1970);
00087         }
00088 
00089         if (!is_null($timezone)){
00090             date_default_timezone_set($oldzone);
00091         }
00092 
00093         return $offset;
00094     }

checkDateTime ( year,
month,
day,
hour = 0,
minute = 0,
second = 0 
)

Deprecated since 1.1.7

Definition at line 204 of file Date.php.

00205     {
00206         if (!checkdate($month, $day, $year)) {
00207             return false;
00208         }
00209         foreach (array('hour' => 23, 'minute' => 59, 'second' => 59) as $var => $maxValue) {
00210             $value = (int)$$var;
00211             if (($value < 0) || ($value > $maxValue)) {
00212                 return false;
00213             }
00214         }
00215         return true;
00216     }

date ( format = null,
input = null 
)

Converts input date into date with timezone offset Input date must be in GMT timezone

Parameters:
string $format
int || string $input date in GMT timezone

Definition at line 120 of file Date.php.

00121     {
00122         if (is_null($format)) {
00123             $format = 'Y-m-d H:i:s';
00124         }
00125 
00126         $result = date($format, $this->timestamp($input));
00127         return $result;
00128     }

getGmtOffset ( type = 'seconds'  ) 

Get current timezone offset in seconds/minutes/hours

Parameters:
string $type
Returns:
int

Definition at line 182 of file Date.php.

00183     {
00184         $result = $this->_offset;
00185         switch ($type) {
00186             case 'seconds':
00187             default:
00188                 break;
00189 
00190             case 'minutes':
00191                 $result = $result / 60;
00192                 break;
00193 
00194             case 'hours':
00195                 $result = $result / 60 / 60;
00196                 break;
00197         }
00198         return $result;
00199     }

gmtDate ( format = null,
input = null 
)

Forms GMT date

Parameters:
string $format
int || string $input date in current timezone
Returns:
string

Definition at line 103 of file Date.php.

00104     {
00105         if (is_null($format)) {
00106             $format = 'Y-m-d H:i:s';
00107         }
00108 
00109         $result = date($format, $this->gmtTimestamp($input));
00110         return $result;
00111     }

gmtTimestamp ( input = null  ) 

Forms GMT timestamp

Parameters:
int || string $input date in current timezone

Definition at line 135 of file Date.php.

00136     {
00137         if (is_null($input)) {
00138             return gmdate('U');
00139         } else if (is_numeric($input)) {
00140             $result = $input;
00141         } else {
00142             $result = strtotime($input);
00143         }
00144 
00145         $date      = Mage::app()->getLocale()->date($result);
00146         $timestamp = $date->get(Zend_Date::TIMESTAMP) - $date->get(Zend_Date::TIMEZONE_SECS);
00147 
00148         unset($date);
00149         return $timestamp;
00150 
00151     }

parseDateTime ( dateTimeString,
dateTimeFormat 
)

Deprecated since 1.1.7

Definition at line 221 of file Date.php.

00222     {
00223         // look for supported format
00224         $isSupportedFormatFound = false;
00225         foreach (array(
00226             // priority is important!
00227             '%m/%d/%y %I:%M' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2, 'h' => 4, 'i' => 5)),
00228             'm/d/y h:i'      => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2, 'h' => 4, 'i' => 5)),
00229             '%m/%d/%y'       => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2)),
00230             'm/d/y'          => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2)),
00231             ) as $supportedFormat => $regRule) {
00232             if (false !== strpos($dateTimeFormat, $supportedFormat, 0)) {
00233                 $isSupportedFormatFound = true;
00234                 break;
00235             }
00236         }
00237         if (!$isSupportedFormatFound) {
00238             Mage::throwException(Mage::helper('core')->__('Date/time format "%s" is not supported.', $dateTimeFormat));
00239         }
00240 
00241         // apply reg rule to found format
00242         $regex = array_shift($regRule);
00243         $mask  = array_shift($regRule);
00244         if (!preg_match($regex, $dateTimeString, $matches)) {
00245             Mage::throwException(Mage::helper('core')->__('Specified date/time "%1$s" do not match format "%2$s".', $dateTimeString, $dateTimeFormat));
00246         }
00247 
00248         // make result
00249         $result = array();
00250         foreach (array('y', 'm', 'd', 'h', 'i', 's') as $key) {
00251             $value = 0;
00252             if (isset($mask[$key]) && isset($matches[$mask[$key]])) {
00253                 $value = (int)$matches[$mask[$key]];
00254             }
00255             $result[] = $value;
00256         }
00257 
00258         // make sure to return full year
00259         if ($result[0] < 100) {
00260             $result[0] = 2000 + $result[0];
00261         }
00262 
00263         return $result;
00264     }

timestamp ( input = null  ) 

Converts input date into timestamp with timezone offset Input date must be in GMT timezone

Parameters:
int || string $input date in GMT timezone

Definition at line 159 of file Date.php.

00160     {
00161         if (is_null($input)) {
00162             $result = $this->gmtTimestamp();
00163         } else if (is_numeric($input)) {
00164             $result = $input;
00165         } else {
00166             $result = strtotime($input);
00167         }
00168 
00169         $date      = Mage::app()->getLocale()->date($result);
00170         $timestamp = $date->get(Zend_Date::TIMESTAMP) + $date->get(Zend_Date::TIMEZONE_SECS);
00171 
00172         unset($date);
00173         return $timestamp;
00174     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:56 2009 for Magento by  doxygen 1.5.8