Magento ouputting incorrect date and time

Magento ouputting incorrect date and time

September 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento ouputting incorrect date and time

Magento: Resolving Incorrect Date/Time Output

You’ve encountered an issue where date/time output is off by exactly four hours once you move beyond the initial index.php file in your Magento store. Let’s dive into this problem and find a clean, core-friendly solution.

Understanding the Root Cause

Magento 1.7.0.2 sets the timezone to UTC by default in its core file app/mage.php:

line 767: date_default_timezone_set('UTC');

This means that any direct use of PHP’s built-in date/time functions like date() or time() will return values based on UTC, not your local timezone.

The Default Magento Approach

The correct way to handle dates within Magento is by using the core/date model and setting the desired timezone through Admin panel configuration:

  1. Go to Admin > System > Configuration.
  2. Under General > Locale Options, set your time zone under Time Zone.

Then in your code, use the following pattern:

$now = Mage::getModel('core/date')->timestamp(time());
echo date('m/d/y h:i:s', $now);

This ensures that you’re working with dates appropriate for your local timezone while respecting Magento’s core structure.

Alternatives to Modifying Core Code

While modifying app/mage.php directly (line 767) is an option, it goes against best practices and could cause issues during upgrades. Here are two cleaner alternatives:

Using a Custom Extension

Create a custom extension that initializes the desired timezone before any other core files load.

  1. Create an early loading observer in your configuration file.
  2. Set the desired timezone with date_default_timezone_set() within this observer.
  3. Make sure it loads before Magento’s core initialization (e.g., priority -10).

Overriding Mage::getModel(‘core/date’)

You can create a custom model that extends Mage_Core_Model_Date and override the timestamp() method to set your desired timezone:

class Your_Extension_Model_Core_Date extends Mage_Core_Model_Date
{
    public function timestamp($ts = null)
    {
        if ($ts === null) {
            $ts = time();
        }
        return parent::timestamp($ts);
    }
}

Then, use this custom model in your code:

$now = Mage::getModel('your_extension/core_date')->timestamp(time());
echo date('m/d/y h:i:s', $now);

Conclusion

To resolve the incorrect date/time output issue in Magento 1.7.0.2 without modifying core files, follow these best practices:

  • Configure your timezone in Admin > System > Configuration.
  • Use Mage::getModel('core/date')->timestamp(time()); for all date/time operations.
  • If needed, create a custom extension or override model to set the desired timezone early.

Remember, keeping your Magento installation clean and free of core modifications ensures easier upgrades and improved security. If you’re looking for expert assistance in complex Magento customizations, feel free to reach out to LaravelCompany.

Magento 1 Extensions Admin Configuration

Our network