Magento Date Timestamp Formatting

I was working on some records in a customer module tonight and ran into the need to format dates the Magento stores in the created_at and updated_at attributes.
The problem I was having was that no matter how I used the PHP date function, I could only get my value to return a date from the year 1970.

If you too get the wrong date after using something like:

$created_at = date("Y-M-D", $product->getCreatedAt());

Then you need to first pass your created_at value through the mage core/date model’s timestamp function, and then pass it in the PHP date function, like so:

$created_at = date("Y-M-D", Mage::getModel("core/date")->timestamp($product->getCreatedAt()));

Worth noting that Y-M-D is more less a regexp for the date function, and can be altered in a number of ways to product the exact date format need (See PHP Date for more information about date formatting).

Also, the created_at and updated_at attributes from Magento carry the date string all the way down to seconds so formatting your date can also include hours minutes and seconds if you desired.


Devin R. Olsen

Devin R. Olsen

Located in Portland Oregon. I like to teach, share and dabble deep into the digital dark arts of web and game development.

More Posts

Follow Me:TwitterFacebookGoogle Plus