Introduction
Being a PHP developer, you are no way hiding from PHP’s date()
function.
In case of Magento, makes an usage of timezone which is configured from the backend (System > Configuration > General > Locale Options > Timezone
) for formatting/displaying date/time.
And this makes the results obtained with the php’s solely date()
function and the Magento’s one a bit different.
For example:
Normal PHP Way
$currentTimestamp = time();
echo $date = date('Y-m-d', $currentTimestamp); //2011-12-11 (current date of the server)
Magento Way
$currentTimestamp = Mage::getModel('core/date')->timestamp(time()); //Magento's timestamp function makes a usage of timezone and converts it to timestamp
echo $date = date('Y-m-d', $currentTimestamp); //The value may differ than above because of the timezone settings.
Since Magento is meant to be for multi-website / multi-lingual /
multi-locale purpose, it’s always a good practice to format the date
using Magento’s date/time function.
AFAIK, there is no need for conversion of date/time while inserting into
the database. The formatting/conversion thing is only done at the
frontend level for displaying purposes.
Some Useful Examples
1. Displaying current date
$currentTimestamp = Mage::getModel('core/date')->timestamp(time());
echo $currentDate = date('Y-m-d', $currentTimestamp);
OR
echo $currentDate = Mage::getModel('core/date')->date('Y-m-d');
2. Formatting any date in any format
$anyDate = '2011-12-11';
$dateTimestamp = Mage::getModel('core/date')->timestamp(strtotime($anyDate));
echo $currentDate = date('d.m.Y', $dateTimestamp);
OR
$anyDate = '2011-12-11';
echo $currentDate = Mage::getModel('core/date')->date('d.m.Y', strtotime($anyDate));
3. Predefined date formatting
$dateToFormat = '2011-12-11';
Mage::helper('core')->formatDate($dateToFormat, 'medium', false);
Note:
Mage_Core_Helper_Data::format() has following arguments
/**
* Format date using current locale options
*
* @param date|Zend_Date|null $date in GMT timezone
* @param string $format (full, long, medium, short)
* @param bool $showTime
* @return string
*/
public function formatDate($date=null, $format='short', $showTime=false)
{
....
}
Hope this gave some info about Date/Time functionality available in Magento.
Thanks for reading!
Cheers!!
来源: http://www.blog.magepsycho.com/playing-with-dates-in-magento/
分享到:
相关推荐
magento批量处理订单的发货,关闭,填写物流单号等状态
He is the Chief Operating Officer of Wagento, a full-service Magento Solution Partner with offices in USA, Mexico, Bolivia, India, and Argentina. He is also a moderator in Magento forums, and he is a...
magento处理不同的头部header
magento数据结构分析 magento数据字典
magento的SMTP插件magento的SMTP插件magento的SMTP插件
[Packt Publishing] Getting Started with Magento Extension Development (E-Book) ☆ 图书概要:☆ Understand Magento extensions, and build your own from scratch! Overview Get a detailed insight into ...
magik shoes magento 模板, magento 1.7 模板
Magento-中文开发教程, 欢迎大家指出错误。 深入理解Magento-第一章 – Magento强大的配置...Magento时间/时区问题 重新安装 Magento 模块 Magnto获取当前店铺和店铺配置的方法 Magento如何重写或新建后台的页面
magento用户中文操作手册,学习magento后台管理
magento2跨域处理包
Chapter 3, Enabling Performance in Magento 2, explains how to configure different types of caching options. In Magento 2, the Full Page Cache (FPC) can be handled by Varnish to give your store a ...
Magento插件开发手册 Magento Extension Developers Guide
深入理解Magento 由精东博客下载下来制作成pdf
官网卖99美元的强大插件Extended Product Grid with Editor本人已经在1.7版本上安装测试过能用
Athlete Magento主题,兼容Magento 1.9.x, 1.8.x, 1.7.x ,特点:完全自适应(响应式布局),自定义子主题,无限颜色,完全自定义,一键安装演示,小工具化横幅系统,颜色选择,MEGA菜单,多商店支持,自定义产品...
主要包括的内容有:magento的常用设置、Magento模板制作教程、Magento商城_目录结构说明、Magento中文教程实用文档、常用的xml的文件的作用以及自定义模块的应用
magento2 developers cookbook, magento 开发手册,magento教程
快速在服务器上复制为另一个站,版本用的是magento,php语言
Magento 的配置系统就像是 Magento 的心脏,支撑着 Magento 的运行。这套 配置系统掌管着几乎所有“module/model/class /template/etc”。它把整个 Magento 系统抽象出来,用一个配置文件来描述。这里的“配置文件”...