Magento date and time functions
Magento date-related methods are mainly in two files. One is the core/date model class, and the other is the core assistant class. The methods are as follows.
Example
Mage: getSingleton ('core/date ')
Mage: getSingleton ('core/date')-> timestamp ($ input)
Obtains the timestamp of a specified date. If the parameter is null, the current timestamp is obtained.
The Returned time is actually the calculation result of the GMT time and the specified time zone in the background.
Mage: getSingleton ('core/date')-> gmtTimestamp ($ input)
Obtain the current GMT timestamp. If the parameter is null, the current GMT timestamp is obtained.
Mage: getSingleton ('core/date')-> date ($ format, $ input)
Obtain the date and time of the specified format and timestamp. If $ format is null, the format is Y-m-d H: I: s. If $ input is null, the current time is returned.
The Returned time is actually the calculation result of the GMT time and the specified time zone in the background.
Mage: getSingleton ('core/date')-> gmtDate ($ format, $ input)
Obtain the GMT Date and time of the specified format and timestamp. If $ format is null, the format is Y-m-d H: I: s. If $ input is null, the current GMT time is returned.
In general, the actual time used for direct display is used:
Mage: getSingleton ('core/date')-> date ($ format)
Equivalent
Mage: getSingleton ('core/date')-> date ($ format, Mage: getSingleton ('core/date')-> gmtTimestamp ())
However, the GMT time should be used for storing data in the database to ensure that each read time is not affected by the time zone settings.
Mage: getSingleton ('core/date')-> gmtDate ('Y-m-d H: I: s ')
The date method can be directly used as the $ input parameter during display:
Mage: getSingleton ('core/date')-> date ('Y-m-d H: I: S', Mage: getSingleton ('core/date ') -> gmtDate ('Y-m-d H: I: s '))
Supplement:
1. core/date: the main method is date, which is actually a simple encapsulation of the date () function in PHP. The definition is as follows.
/**
* Converts input date into date with timezone offset
* Input date must be in GMT timezone
*
* @ Param string $ format
* @ Param int | string $ input date in GMT timezone
* @ Return string
*/
Public function date ($ format = null, $ input = null)
{
If (is_null ($ format )){
$ Format = 'Y-m-d H: I: s ';
}
$ Result = date ($ format, $ this-> timestamp ($ input ));
Return $ result;
}
For example, if the date () parameter is null, the default parameter is Y-m-d H: I: s, and parameter 2 is the current timestamp.
Mage: getModel ('core/date')-> date (); // 23:16:10
Mage: getModel ('core/date')-> date ('Y-m-D'); // 2013-01-09
$ AnyDate = '2017-01-09 ';
Mage: getModel ('core/date')-> date ('D. m. Y', strtotime ($ anyDate); // 09.01.2013
2. The formatDate method of the core assistant class, which is defined as follows.
/**
* Format date using current locale options and time zone.
*
* @ Param date | Zend_Date | null $ date
* @ Param string $ format See Mage_Core_Model_Locale: FORMAT_TYPE _ * constants
* @ Param bool $ showTime Whether to include time
* @ Return string
*/
Public function formatDate ($ date = null, $ format = Mage_Core_Model_Locale: FORMAT_TYPE_SHORT, $ showTime = false)
{
If (! In_array ($ format, $ this-> _ allowedFormats, true )){
Return $ date;
}
If (! ($ Date instanceof Zend_Date) & $ date &&! Strtotime ($ date )){
Return '';
}
If (is_null ($ date )){
$ Date = Mage: app ()-> getLocale ()-> date (Mage: getSingleton ('core/date')-> gmtTimestamp (), null, null );
} Else if (! $ Date instanceof Zend_Date ){
$ Date = Mage: app ()-> getLocale ()-> date (strtotime ($ date), null, null );
}
If ($ showTime ){
$ Format = Mage: app ()-> getLocale ()-> getDateTimeFormat ($ format );
} Else {
$ Format = Mage: app ()-> getLocale ()-> getDateFormat ($ format );
}
Return $ date-> toString ($ format );
}
For example, parameter 2 supports four parameters, as shown below. If parameter 3 is true, the output contains the current time.
$ DateToFormat = '2017-01-09 ';
Mage: helper ('core')-> formatDate ($ dateToFormat, 'full', false); // Tuesday, January 9, 2013
Mage: helper ('core')-> formatDate ($ dateToFormat, 'long', false); // January 9, 2013
Mage: helper ('core')-> formatDate ($ dateToFormat, 'Medium ', false); // Jan 9, 2013
Mage: helper ('core')-> formatDate ($ dateToFormat, 'short ', false); // 1/9/13