Magento format DateTime using Configuration options.Playing with magento date and time comparing with php date and time format.
Magento format DateTime using Configuration options.

In this tutorial I am going show you how we can manage Magento format DateTime using Configuration options.In magento we don’t need to define time zone through our code side as it can declare in backend side by the dir System => configuration => General => Locale Option => Timezone.lets talk for Magento format DateTime using Configuration options.Before to explain everything about magento date and time formatting I would like show you the core function of magento. Which we should call for formatting our date and time if we will format Magento format DateTime using Configuration options for our custom code or custom module.

If you navigate dir : app/code/core/Mage/core/Helper/Data.php then you will getting two function one is for date formatting called formatDate() and other is formatting time called formatTime().So if you understand below two functions well then your most of problem on Working with php and magento dates is resolved now.In the same way we can refer model class of date for other date and time formatting functions.If you will navigate through dir : app/code/core/Mage/core/Model/Date.php

FormatDate Function:

< ?php
public function formatDate($date = null, $format = Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, $showTime = false)
{
    ............................
    ............................
    ............................
}
?>

formatTime Function:

< ?php
public function formatTime($time = null, $format = Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, $showDate = false)
{
    ............................
    ............................
    ............................
}
?>

Above two functions are core functions I have shown here for your references not to edit direct to these functions of helper file.What we need to do is we just need to these function wherever we want.Lets see how we can call on different situations.

Current Default Type Date Format:

< ?php
echo $currentDate = Mage::getModel('core/date')->date();
?>
Out Put:

2015-03-28 11:24:32

Current Formatted Date Format:

< ?php
echo $currentDate = Mage::getModel('core/date')->date('Y-m-d');
?>
Out Put:

2015-03-28

Formatted Type Date Format:

< ?php
$tomorrow = '2019-03-29';
$dateTimestamp = Mage::getModel('core/date')->timestamp(strtotime($tomorrow));
echo date('d.m.Y', $dateTimestamp);
?>

Or

< ?php
echo $currentDate = Mage::getModel('core/date')->date('d.m.Y', strtotime($tomorrow));
?>
Out Put:

28.03.2019

Above two different examples are for formatting date format own your desire format.But if you will like to use only in magento way then see below examples where I have explained how magento uses different date format.

Default date format:

Medium Type
< ?php
$today = '2015-03-28';
$date = Mage::helper('core')->formatDate($today, Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, false);
echo $date;
?>
Out Put:

Mar 28, 2015

Short Type
< ?php
$today = '2015-03-28';
$date = Mage::helper('core')->formatDate($today, Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, false);
echo $date;
?>
Out Put:

3/28/15

Long Type
< ?php
$today = '2015-03-28';
$date = Mage::helper('core')->formatDate($today, Mage_Core_Model_Locale::FORMAT_TYPE_LONG, false);
echo $date;
?>
Out Put:

March 28, 2015

Full Type
< ?php
$today = '2015-03-28';
$date = Mage::helper('core')->formatDate($today, Mage_Core_Model_Locale::FORMAT_TYPE_FULL, false);
echo $date;
?>
Out Put:

Friday, March 28, 2015

In same way you can use to formatting time with magento default format replacing formatDate function to formatTime function like.

Default time format:

Medium Type
< ?php
$today = '2015-03-28';
$date = Mage::helper('core')->formatTime($today, Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, false);
echo $date;
?>
Out Put:

4:00:00 PM

Short Type
< ?php
$today = '2015-03-28';
$date = Mage::helper('core')->formatTime($today, Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, false);
echo $date;
?>
Out Put:

5:00 PM

Long Type
< ?php
$today = '2015-03-28';
$date = Mage::helper('core')->formatTime($today, Mage_Core_Model_Locale::FORMAT_TYPE_LONG, false);
echo $date;
?>
Out Put:

5:00:00 PM PDT

Full Type
< ?php
$today = '2015-03-28';
$date = Mage::helper('core')->formatTime($today, Mage_Core_Model_Locale::FORMAT_TYPE_FULL, false);
echo $date;
?>
Out Put:

5:00:00 PM America/Los_Angeles

All the results are coming due to configuration of magento in backend.

Magento format DateTime using Configuration options
Tagged on:                     

3 thoughts on “Magento format DateTime using Configuration options

  • October 22, 2016 at 3:27 am
    Permalink

    Hi,
    How do i restrict date range in custom option and disable old dates in calendar for custom option ?

    Thanks

    Reply
    • October 22, 2016 at 9:31 am
      Permalink

      Can you please provide the screen shot where you want to restrict date range ? However you can restrict date range in JavaScript(prototype) just like jquery date picker.

      Reply
  • November 15, 2016 at 12:59 pm
    Permalink

    Here i founded one bug in your Time Format. Please replace from “formatDate” to “formatTime” on Different Time format.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *