How to add custom date in order history comment in magento programmatically?

How to add custom date in order history comment in magento programmatically?

September 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add custom date in order history comment in magento programmatically?

Programmatically Adding Custom Date to Order Comment History in Magento

As an experienced developer working with Magento, I often encounter requests for customizing order history comments with specific dates. While the default behavior adds the current date and time, there are ways to insert a custom date programmatically. Let’s explore this process step-by-step.

Understanding the existing code

You’re currently attempting to set a custom date using the following snippet:

$new_date = '2010-01-01';
$order->setData('created_at', $new_date);
$order->addStatusToHistory($order->getStatus(), 'This comment is programatically added', false);
$order->save();

The issue lies in the setData method. It’s used to update various data attributes of an order, but it doesn’t directly affect the timestamp associated with the comments.

Getting the correct date format

When dealing with dates in Magento, ensure you’re using the correct format. By default, Magento expects dates in the following Y-m-d H:i:s format:

2022-12-31 23:59:59

For your custom date of ‘2010-01-01’, it should be reformatted to:

$new_date = '2010-01-01 00:00:00';

Using the correct method for comment history

To add a status history with a custom timestamp, you need to use the setCreatedAt method instead of setData.

Here’s the corrected code snippet:

$new_date = '2010-01-01 00:00:00';
$order->addStatusToHistory(
    $order->getStatus(),
    'This comment is programatically added with a custom date',
    false,
    $new_date
);

In this updated version, the fourth parameter $timestamp is used to specify the custom date and time for the status history record. By providing this timestamp when calling addStatusToHistory, Magento will use it instead of the current system time.

Best practices for adding order comments

While implementing this solution:

  1. Always ensure you have proper read/write permissions on the order entity.
  2. Use try-catch blocks around your database operations to handle any potential exceptions gracefully.

Remember, working with order data requires careful handling due to its critical nature in e-commerce transactions.

Conclusion

Adding custom dates to Magento order history comments programmatically is possible by utilizing the addStatusToHistory method and providing a specific timestamp. By modifying the existing code as shown above, you can accurately associate your manually added comments with the desired date and time.

If you require further assistance with Magento development or need expert guidance on other e-commerce platforms like Shopify Plus or Salesforce Commerce Cloud, feel free to explore our services at Laravel Company.

Happy coding!

Orders Errors

Our network