最新下载
热门教程
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 
Magento日期及时间函数详细介绍
时间:2022-06-25 00:47:16 编辑:袖梨 来源:一聚教程网
Magento日期及时间函数
Magento日期相关的方法主要在两个文件中。一个是core/date模型类,另外一个是core核心助手类。包含的相关方法如下。
例子
Mage::getSingleton('core/date')
Mage::getSingleton('core/date')->timestamp( $input )
获取指定日期的时间戳。若参数为空则获取当前时间戳。
这里返回的时间实际上是 GMT 时间与后台 Timezone 设置所指定时区的计算结果。
Mage::getSingleton('core/date')->gmtTimestamp( $input )
获取当前 GMT 时间戳。若参数为空则获取当前 GMT 时间戳。
Mage::getSingleton('core/date')->date( $format, $input )
获取指定格式及时间戳的日期时间。若 $format 为空则格式为 Y-m-d H:i:s;若 $input 为空则返回当前时间。
这里返回的时间实际上是 GMT 时间与后台 Timezone 设置所指定时区的计算结果。
Mage::getSingleton('core/date')->gmtDate( $format, $input )
获取指定格式及时间戳的 GMT 日期时间。若 $format 为空则格式为 Y-m-d H:i:s;若 $input 为空则返回当前 GMT 时间。
 
一般来说,实际应用中用于直接显示的时间都使用:
Mage::getSingleton('core/date')->date( $format )
等价于
Mage::getSingleton('core/date')->date( $format, Mage::getSingleton('core/date')->gmtTimestamp() )
 
而用于储存在数据库的时间应该使用 GMT 时间,以保证每次读取的时间都不受时区设置所影响
Mage::getSingleton('core/date')->gmtDate( 'Y-m-d H:i:s' )
显示时可作为 $input 参数直接使用 date 方法:
Mage::getSingleton('core/date')->date( 'Y-m-d H:i:s', Mage::getSingleton('core/date')->gmtDate( 'Y-m-d H:i:s' ) )
补充:
1,core/date,主要的方法就是date,实际上就是简单对PHP中date()函数的封装。定义如下。
/**
* 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;
}
使用举例,date()参数为空时,默认参一为Y-m-d H:i:s,参二为当前时间戳。
Mage::getModel('core/date')->date();                        //2013-01-09 23:16:10
Mage::getModel('core/date')->date('Y-m-d');                 //2013-01-09
 
$anyDate = '2013-01-09';
Mage::getModel('core/date')->date('d.m.Y', strtotime($anyDate));        //09.01.2013
2,核心助手类formatDate方法,该方法定义如下。
/**
* 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);
}
实例演示,参数二支持4个参数,如下所示,参数三为true时,则输出包含当前时间。
$dateToFormat = '2013-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
相关文章
- 过山车之星2游戏收费设置技巧分享 11-04
 - 大侠立志传白猿白首怎么领养-白猿白首领养攻略 11-04
 - 炉石传说标准模式假人战卡组推荐分享 11-04
 - 炉石传说行星萨高爆发OTK卡组分享 11-04
 - 炉石传说标准模式元素法卡组推荐分享 11-04
 - 炉石传说烟花元素法上分卡组推荐分享 11-04